I have a couple of questions below and would really appreciate some light 
on the way to handle the situations described below using the REST 
framework where a user can be posting from a web form where we know that we 
intend to happen or via a cli where we do no know that the user intends to 
happen.

If I have a SerializerClass that has a reference to a m2m (shown below)

1. When a PUT request is sent to update the record and no data for the m2m 
relationship was sent with the request should the update method expect that 
no data was posted for that m2m so the user intended for them to be 
removed. Or does the user expect them to be left in place when not posting 
any data for the m2m? I would think the user expects them to be left in 
place. But if that is the case, then how would the user signify that he 
intends for them to be removed on an update? Should that be handled with a 
delete request or a new resource url be created for this purpose?

2. If the user does send data for the m2m relationship in the PUT request 
ie a list with one item categories=[{"category":"test"}] are they expecting 
the REST api update method update the relationship to only contain the one 
element and remove the rest, or are they expecting to keep the existing 
records and just add the extra relation? 

So should a PUT request be able to delete m2m relations on that record or 
should it only be able to ever add to existing relations and a new resource 
url should be used to remove the m2m relations?

Example code below.

Thanks


class QuestionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Question
        fields = ('pk', 'question', 'user', 'url', "categories")

    categories = CategorySerializer(many=True, required=False)    
    user = serializers.ReadOnlyField(source='user.username')

    def update(self, instance, validated_data):
        q = validated_data.get('question', instance.question).strip()
            
        instance.question = q
        instance.save()
        
        #1.  If an empty list is passed in from a form or cli or no data at 
all, does this signify that they should be removed or left in place. If 
they should be left alone, do we create a new
       #resource url to delete them
       
         #2.  If 1 category is passed in a list, does that mean the rest of 
the categories get removed and we just add the new one
       # or does it mean we leave the categories in place and add the new 
one to the existing categories?
        categories = validated_data.get('categories')
        if categories:
            #instance.categories.clear() # should we clear out or not?
            for category in categories:
                c = 
Category.objects.get(Q(user=self.context['request'].user) | Q(user=None),
                                         **category)
                instance.categories.add(c)
        return instance

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to