我正在尝试创建一个可写的嵌套序列化程序。 我读了django rest文档,但在我的情况下,我有多个嵌套字段,不知道如何解决我得到这个错误:
/ api / v1 / libraries /中的TypeError 'tel_number'是此函数的无效关键字参数
models.py:
class RegisterRule(models.Model):
picture = models.BooleanField(default=False)
passport = models.BooleanField(default=False)
education_rule = models.CharField(max_length=255)
class SpecificInformatin(models.Model):
specific_name = models.CharField(max_length=255)
description = models.TextField(max_length=255)
def __unicode__(self):
return self.specific_name
class District(models.Model):
district = models.CharField(max_length=2)
def __unicode__(self):
return self.district
class Address(models.Model):
district = models.ForeignKey(District, on_delete=models.CASCADE)
square = models.CharField(max_length=20)
master_street = models.CharField(max_length=20)
slave_street = models.CharField(max_length=20)
plaque = models.CharField(max_length=3)
def __unicode__(self):
return self.slave_street
class Library(models.Model):
name = models.CharField(max_length=30)
address = models.OneToOneField(Address, on_delete=models.CASCADE, related_name='address')
reading_room = models.BooleanField(default=False)
reading_room_start_time = models.CharField(max_length=10, default='8:00')
reading_room_end_time = models.CharField(max_length=10, default='20:00')
library_start_time = models.CharField(max_length=10, default='8:00')
library_end_time = models.CharField(max_length=10, default='20:00')
specific_information = models.ManyToManyField(SpecificInformatin)
register_rules = models.ManyToManyField(RegisterRule)
gender_days_in_week = models.CharField(max_length=100, default='mens')
manager_of_library = models.CharField(max_length=50, default='shahrdari')
tel_number = models.CharField(max_length=11, null=True)
email = models.EmailField(max_length=40, default="@.com")
from_user = models.BooleanField(default=False)
def __unicode__(self):
return self.name
serializers.py:
class LibrarySerializer(serializers.ModelSerializer):
address = AddressSerializer()
specific_information = SpecificSerializer(many=True)
register_rules = RegisterRulesSerialzers(many=True)
class Meta:
model = Library
fields = ('id', 'name', 'address', 'reading_room', 'reading_room_start_time'
, 'reading_room_end_time', 'library_start_time', 'library_end_time'
, 'specific_information', 'register_rules', 'gender_days_in_week'
, 'manager_of_library', 'tel_number', 'email', 'from_user')
def create(self, validated_data):
address_data = validated_data.pop('address')
address = Address.objects.create(**validated_data)
register_rules_data = validated_data.pop('register_rules')
register_rules = RegisterRule.objects.create(**validated_data)
specific_information_data = validated_data.pop('specific_information')
specific_information = SpecificInformatin.objects.create(**validated_data)
library = Library.objects.create(**validated_data)
library.address(address=address, **address_data)
for data in register_rules_data:
library.register_rules.add(register_rules=register_rules, **data)
for data in specific_information_data:
library.specific_information.add(specific_information=specific_information, **data)
return library
答案 0 :(得分:2)
你犯了一些错误。当您从dict弹出一些数据时,它会返回特定数据的值。
create(self, validated_data):
address_data = validated_data.pop('address')
address = Address.objects.create(**address_data)
register_rules_data = validated_data.pop('register_rules')
specific_information_data = validated_data.pop('specific_information')
library = Library.objects.create(address=address, **validated_data)
for data in register_rules_data:
register_rules = RegisterRule.objects.create(**data)
library.register_rules.add(register_rules)
for data in specific_information_data:
specific_information = SpecificInformatin.objects.create(**data)
library.specific_information.add(specific_information)
return library
我认为此代码适合您。