json - depth = 1 doesn't work properly and it's saves Null in ManyToManyField and ForeignKey fields in Django Rest Framework -


after adding depth = 1 doesn't work properly

=> 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 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 

i have added foreignkey , manytomanyfield fields in state , city respectively. doesn't save values in snippetserializer while added depth = 1 in meta class (it saves null value in state , city fields). when add depth = 1 json showing related fields should doesn't work while add new snippet. without depth = 1 works fine. have complex database tables has many manytomany , foreignkey related fields. please give me suggestion can related data in json.

i have djangorestframework-3.1.2 version. have used latest version same problem. please give me solution , in advance.

depth representation (http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization). if want create/update related fields have overwrite create/update methods in serializer (http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers).

greetings.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -