Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django REST框架:使用嵌套对象值(而不是主键)创建和更新对象_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django REST框架:使用嵌套对象值(而不是主键)创建和更新对象

Python Django REST框架:使用嵌套对象值(而不是主键)创建和更新对象,python,django,django-rest-framework,Python,Django,Django Rest Framework,我有两种型号一个国家一个办公室。office模型具有国家/地区模型的外键: class Country(TranslatableModel): iso = models.CharField( max_length=2, verbose_name=_('iso code'), help_text="ISO 3166 ALPHA-2 code") translations = TranslatedFields( name=models.

我有两种型号一个国家一个办公室。office模型具有国家/地区模型的外键:

class Country(TranslatableModel):
    iso = models.CharField(
        max_length=2, verbose_name=_('iso code'),
        help_text="ISO 3166 ALPHA-2 code")
    translations = TranslatedFields(
        name=models.CharField(max_length=100, verbose_name=_('name')),
    )



class Office(models.Model):
    country = models.ForeignKey(
        Country, related_name='country', verbose_name=_('country'))
现在我想编写一个django rest框架序列化程序,只需发送
{“country”:“us”}
即可获得country模型的ForeingKey

如何实现这一点?

只读 只需将该表示发送到客户端(只读,不处理从其反序列化表示创建对象)

如您所见,它将从您的
office
实例中读取
country.iso
,该实例解析为
'us'
,例如,如果您的输出为
{'country':'us'
,它将被放入名为
'country'
的序列化程序键中

可写嵌套字段 现在,为了完成此任务,让我们编写一个自定义的
OfficeSerializer.create()

至于
OfficeSerializer.update()
它是类似的:

def update(self, instance, validated_data):
    # instance is your Office object
    # You should update the Office fields here
    # instance.field_x = validated_data['field_x']

    # Let's grab the Country object again

    country_iso = validated_data.pop('country')
    country = Country.objects.get(iso=country_iso)

    # Update the Office object
    instance.country = country
    instance.save()

    return instance

是的,我知道如何显示国家代码,但如何通过POST/PUT API更新国家代码?(用户只需设置国家代码,无需ID,…)
def create(self, validated_data):
    # This expects an input format of {"country": "iso"}

    # Instead of using the ID/PK of country, we look it up using iso field
    country_iso = validated_data.pop('country')
    country = Country.objects.get(iso=country_iso)

    # Create the new Office object, and attach the country object to it
    office = Office.objects.create(country=country, **validated_data)

    # Notice I've left **validated_data in the Office object builder,
    # just in case you want to send in more fields to the Office model

    # Finally a serializer.create() is expected to return the object instance
    return office
def update(self, instance, validated_data):
    # instance is your Office object
    # You should update the Office fields here
    # instance.field_x = validated_data['field_x']

    # Let's grab the Country object again

    country_iso = validated_data.pop('country')
    country = Country.objects.get(iso=country_iso)

    # Update the Office object
    instance.country = country
    instance.save()

    return instance