【发布时间】:2018-05-17 13:06:39
【问题描述】:
当“fk_tipo_entrante”等于“2”时,我想获取所有“Registro_Entrada”并通过“Entrante”过滤它们。
在 VIEWS.py 我有:
class GetPersonalEntranteRegistro_EntradaAPIView(generics.ListAPIView):
serializer_class = Registro_EntradaNestedSerializer
def get_queryset(self):
entrante = Entrante.objects.all().filter(fk_tipo_entrante=2)
registro = Registro_Entrada.objects.filter(fk_entrante = entrante.pk)
return registro
在 SERIALIZERS.py 我有:
class Registro_EntradaNestedSerializer(serializers.ModelSerializer):
fk_tarifa = TarifaSerializer(many=False)
fk_entrante = EntranteNestedSerializer(many=False)
class Meta:
model = Registro_Entrada
fields = ('_all_')
在 MODELS.py 上我有:
class Entrante(models.Model):
fk_credencial = models.ForeignKey(Credencial,related_name='credencial_entrada',on_delete=models.CASCADE)
fk_placa = models.ForeignKey(Placa, related_name='credencial_placa',on_delete=models.CASCADE)
fk_tipo_entrante = models.ForeignKey(Tipo_Entrante,related_name='entrante_tipo',on_delete=models.CASCADE)
fk_persona = models.ForeignKey(Persona,related_name='entrante_persona',on_delete=models.CASCADE)
estatus = models.IntegerField(default=1)
class Registro_Entrada(models.Model):
fecha_entrada = models.DateTimeField(auto_now_add=True)
fecha_salida = models.DateTimeField(null=True)
fk_tarifa = models.ForeignKey(Tarifa,related_name='registro_tarifa',on_delete=models.CASCADE)
fk_entrante = models.ForeignKey(Entrante,related_name='regitro_entrante',on_delete=models.CASCADE)
costo = models.DecimalField(decimal_places=2, max_digits=50)
saldo = models.DecimalField(decimal_places=2,max_digits=50)
estatus = models.IntegerField(default=1)
它显示以下错误:
'QuerySet' 对象没有属性 'pk'
【问题讨论】:
-
entrante = Entrante.objects.all().filter(fk_tipo_entrante=2).first()
标签: python django filter nested django-rest-framework