如何使用嵌套的串行器我会遇到属性错误



我正在获得属性错误

accounts app name
models.py
class AddressUser(models.Model):
    customer = models.OneToOneField(Customer_create, related_name='customer_address', on_delete=models.CASCADE, blank = True,null=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank = True,null=True)
    house_number = models.CharField(max_length=250)
    builiding_name = models.CharField(max_length=250)
    block_no = models.CharField(max_length=250)
    street = models.CharField(max_length=250)
    area = models.CharField(max_length=250)
    city = models.CharField(max_length=250)
    pincode = models.CharField(max_length=250)
    state_choices = (
        ("Andhra Pradesh", "Andhra Pradesh"), ("Arunachal Pradesh ", "Arunachal Pradesh "), ("Assam", "Assam"),
        ("Bihar", "Bihar"), ("Chhattisgarh", "Chhattisgarh"), ("Goa", "Goa"), ("Gujarat", "Gujarat"),
        ("Haryana", "Haryana"), ("Himachal Pradesh", "Himachal Pradesh"), ("Jammu and Kashmir ", "Jammu and Kashmir "),
        ("Jharkhand", "Jharkhand"), ("Karnataka", "Karnataka"), ("Kerala", "Kerala"), ("Madhya Pradesh", "Madhya Pradesh"),
        ("Maharashtra", "Maharashtra"), ("Manipur", "Manipur"), ("Meghalaya", "Meghalaya"), ("Mizoram", "Mizoram"),
        ("Nagaland", "Nagaland"), ("Odisha", "Odisha"), ("Punjab", "Punjab"), ("Rajasthan", "Rajasthan"),
        ("Sikkim", "Sikkim"), ("Tamil Nadu", "Tamil Nadu"), ("Telangana", "Telangana"), ("Tripura", "Tripura"),
        ("Uttar Pradesh", "Uttar Pradesh"), ("Uttarakhand", "Uttarakhand"), ("West Bengal", "West Bengal"),
        ("Andaman and Nicobar Islands", "Andaman and Nicobar Islands"), ("Chandigarh", "Chandigarh"),
        ("Dadra and Nagar Haveli", "Dadra and Nagar Haveli"), ("Daman and Diu", "Daman and Diu"),
        ("Lakshadweep", "Lakshadweep"), ("National Capital Territory of Delhi", "National Capital Territory of Delhi"),
        ("Puducherry", "Puducherry"))
    state = models.CharField(choices=state_choices, max_length=255)
    country = models.CharField(max_length=10, default='India')
Simple app
model.py
class Customer_create(models.Model):
    #address = models.ForeignKey(AddressUser, on_delete= models.CASCADE)
    customer_name=models.CharField(max_length=250)
    mobile_number=models.CharField(max_length=10)
    email=models.CharField(max_length=50)
serializers.py
class AddressSerlizer(serializers.ModelSerializer):
    class Meta:
        model = AddressUser
        fields = ('id','house_number','builiding_name','block_no', 'street', 'area', 'city', 'pincode', 'state', 'country',)
class Customer_serializer(serializers.ModelSerializer):
    address = AddressSerlizer(many=True, allow_null=False)
    class Meta:
        model = Customer_create
        fields = ('id','customer_name',
                'mobile_number',
                'email',
                'address',)
    # def create(self, validated_data):
    #     """
    #     Overriding the default create method of the Model serializer.
    #     :param validated_data: data containing all the details of student
    #     :return: returns a successfully created student record
    #     """
    #     user_data = validated_data.pop('address')
    #     user = AddressSerlizer.create(AddressSerlizer(), validated_data=user_data)
    #     student, created = Customer_create.objects.create(user=user,
    #                                                     customer_name = validated_data.pop('customer_name'),
    #                                                   mobile_name=validated_data.pop('mobile_number'),
    #                                                   email=validated_data.pop('email'))
    #     return student
    def create(self, validated_data):
        address_data = validated_data.pop('address')
        # customer = Customer_create.objects.create(**validated_data)
        # AddressUser.objects.create(customer=customer, **address_data)
        # return customer
        # customer = Customer_create(customer_name = validated_data['customer_name'],
        #                            mobile_name = validated_data['mobile_number'],
        #                            email = validated_data['email']
        #                            )
        # customer.save()
        customer = Customer_create.objects.create(**validated_data)
        for i in address_data:
            AddressUser.objects.create(customer=customer, **i)
        return customer
AttributeError at /simple/customer_post

尝试在序列化器Customer_serializer上获得字段address的值时获得属性。 序列化器字段可能被错误地命名,并且在Customer_create实例上不匹配任何属性或键。 原始异常文本是:'customer_create'对象没有属性" address"。

**

这是因为您需要指定Customer_create模型和AddressUser模型之间的关系。您可以从下面的Customer_create模型中删除以下注释。因为DRF序列化器无法在Customer_create模型的实例中找到address

class Customer_create(models.Model):
    #address = models.ForeignKey(AddressUser, on_delete= models.CASCADE)
    customer_name=models.CharField(max_length=250)
    mobile_number=models.CharField(max_length=10)
    email=models.CharField(max_length=50)

比运行python manage.py makemigrationspython manage.py migrate命令

相关内容

最新更新