Django Rest Framework - 嵌套序列化不起作用



model.py

class Account(models.Model):
name_Account= models.CharField(max_length=50, default='')
fecha_nacimiento = models.CharField(max_length=150, default='')
phone = models.CharField(max_length=150, default='')
mail = models.CharField(max_length=150, default='')
user_id = models.ForeignKey(User,on_delete=models.CASCADE)
rol_id = models.ForeignKey(Rol,on_delete=models.CASCADE, null =True)
class Reclamo(models.Model):
nameReclamo= models.CharField(max_length=50, default='')
rut = models.CharField(max_length=20, default='')
numpoliza = models.CharField(max_length=30, default='')
detalle_diagnostico = models.CharField(max_length=200, default='')
account_id = models.ForeignKey(Account,on_delete=models.CASCADE,null =True)
date = models.DateField(auto_now=True)
name_estado= models.CharField(max_length=50, default='Pendiente')
num_claim= models.CharField(max_length=30, default=' ' ,blank = True)

serializer.py

class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ('id', 'name_Account', 'fecha_nacimiento', 'phone', 'mail', 'user_id', 'rol_id')
class ReclamoSerializer(serializers.ModelSerializer):
name_Account = AccountSerializer(many=False) #read_only=True no return, no error 
class Meta:
model = Reclamo
fields = ('id','nameReclamo','rut','numpoliza','detalle_diagnostico','account_id','date','name_estado','num_claim', 'name_Account')  

错误

尝试获取字段值时出错name_Account在序列化程序ReclamoSerializer上。 序列化程序字段可能命名不正确,并且与Reclamo实例上的任何属性或键都不匹配。 原始异常文本为:"Reclamo"对象没有属性"name_Account"。

我尝试了不同的例子,但我无法返回我希望的

属性name_Account不存在。您必须指向account_id,因为这是Account模型的ForeignKey

class ReclamoSerializer(serializers.ModelSerializer):
account_id = AccountSerializer(many=False)
class Meta:
model = Reclamo
fields = ('id','nameReclamo','rut','numpoliza','detalle_diagnostico','account_id','date','name_estado','num_claim')

相关内容

  • 没有找到相关文章

最新更新