Cross-posted from SO - apologies if that's bad form!
I am PATCHing a model using DRF, and the call is, for some reason, wiping a ManyToMany field. Why is this?
I have a Feature model:
class Feature(CommonInfo):
user = models.ForeignKey(User)
name = models.CharField(max_length=50)
parent = models.ForeignKey("Feature", blank=True, null=True, related_name='children')
tags = models.ManyToManyField("Tag")
level = models.IntegerField()
box_image = ImageField()
background_image = ImageField()...and a serializer:
class FeatureSerializer(serializers.HyperlinkedModelSerializer):
user = serializers.ReadOnlyField(source='user.username')
children = serializers.HyperlinkedRelatedField(read_only=True, view_name='feature-detail', many=True)
level = serializers.ReadOnlyField()
def get_fields(self, *args, **kwargs):
user = self.context['request'].user
fields = super(FeatureSerializer, self).get_fields(*args, **kwargs)
fields['parent'].queryset = fields['parent'].queryset.filter(user=user)
return fields
class Meta:
model = Feature...and a viewset:
class FeatureViewSet(viewsets.ModelViewSet):
serializer_class = FeatureSerializer
def get_queryset(self):
return self.request.user.feature_set.order_by('level', 'name')
def perform_create(self, serializer):
serializer.save(user=self.request.user)This gives an output for GET /api/features/5/:
{
"url": "http://localhost:8001/api/features/5/",
"user": "andrew",
"children": [],
"level": 1,
"created_at": "2015-02-03T15:11:00.191909Z",
"modified_at": "2015-02-03T15:20:02.038402Z",
"name": "My Astrantia major 'Claret' plant",
"box_image": "http://localhost:8001/media/Common_Knapweed13315723294f5e2e6906593_PF07bKi.jpg",
"background_image": null,
"parent": "http://localhost:8001/api/features/1/",
"tags": [
"http://localhost:8001/api/tags/256/"
]
}Suppose I want to run a PATCH call to update name:
import requests
r = requests.patch("http://localhost:8001/api/features/5/",
data={'name':'New name'},
auth=('user', 'password'))
r.json()This successfully updates the object, but the result also wipes the tags from the object:
{
"url": "http://localhost:8001/api/features/5/",
"user": "andrew",
"children": [],
"level": 1,
"created_at": "2015-02-03T15:11:00.191909Z",
"modified_at": "2015-02-03T16:12:48.055527Z",
"name": "New name",
"box_image": "http://localhost:8001/media/Common_Knapweed13315723294f5e2e6906593_PF07bKi.jpg",
"background_image": null,
"parent": "http://localhost:8001/api/features/1/",
"tags": []
}--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/GeAHrgJ-YoI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.
{ "id": 1, "name": "sample name 1", "persons": [ 1 ] },When POSTed at the same serializer, in the .create() method, validated_data is:
{ "id": 1, "name": "sample name 1", "persons": [ ] },
class Meta:
model = Feature
exclude = ('tags', 'box_image', 'banner_image')
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.