depth = 1不能正常工作,并且在Django Rest Framework的ManyToManyField和ForeignKey字段中保存了Null [英] depth = 1 doesn't work properly and it's saves Null in ManyToManyField and ForeignKey fields in Django Rest Framework

查看:39
本文介绍了depth = 1不能正常工作,并且在Django Rest Framework的ManyToManyField和ForeignKey字段中保存了Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加深度= 1后无法正常工作

after adding depth = 1 doesn't work properly

=> models.py文件

=> models.py file

class State(models.Model):
    state_name = models.CharField(max_length = 30, unique=True)

    def __unicode__(self):
        return str(self.state_name)

class City(models.Model):
    state = models.ForeignKey(State, related_name='state_city')
    city_name = models.CharField(max_length = 30)

    def __unicode__(self):
        return str(self.city_name)

    class Meta:
        ordering = ('city_name',)
        unique_together = ('state', 'city_name',)

class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    owner = models.ForeignKey('auth.User', related_name='snippets')
    state = models.ForeignKey(State,blank=True,null=True)
    city = models.ManyToManyField(City)

=> serializers.py文件

=> serializers.py file

class StateSerializer(serializers.ModelSerializer):
    class Meta:
        model = State

class CitySerializer(serializers.ModelSerializer):
    state_name = serializers.CharField(source='state.state_name', read_only=True)
    class Meta:
        model = City

class SnippetSerializer(serializers.ModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username', read_only=True)
    class Meta:
        model = Snippet
        fields = ('id', 'title', 'code', 'linenos', 'owner', 'state', 'city')
        depth = 1

我分别在州和城市中添加了ForeignKey和ManyToManyField字段.在元类中添加depth = 1时,它不会在SnippetSerializer中保存值(它会在州和城市字段中保存Null值).当我添加depth = 1时,JSON应该显示相关字段,但是在添加新代码段时无法正常工作.如果深度= 1,则效果很好.我有一个复杂的数据库,其中表具有许多ManyToMany和ForeignKey相关字段.请给我建议,以便我可以使用JSON获取相关数据.

I have added ForeignKey and ManyToManyField fields in state and city respectively. It doesn't save values in SnippetSerializer while added depth = 1 in Meta Class (it saves Null value in state and city fields). When I add depth = 1 JSON showing related fields as it should be but it doesn't work properly while add new Snippet. Without depth = 1 it works fine. I have complex database where tables has many ManyToMany and ForeignKey related fields. Please give me suggestion so I can get related data in JSON.

我有djangorestframework-3.1.2版本.我也使用了最新版本,但存在相同的问题.请给我解决方案,谢谢.

I have djangorestframework-3.1.2 version. I have used latest version too but same problem. please give me solution and thanks in advance.

推荐答案

depth 仅用于表示形式(

depth is only for representation (http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization). If you want to create/update the related fields too you have to overwrite the create/update methods in the serializer (http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers).

问候.

这篇关于depth = 1不能正常工作,并且在Django Rest Framework的ManyToManyField和ForeignKey字段中保存了Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆