Hi,

First thing you should note, it’s not just about ManyToMany relation here but 
rather about nested serializers.


> Le 13 août 2016 à 12:16, Jenna Pullen <[email protected]> a écrit :
> 
> 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?

My usual take on this:
- sending a null value means the field is ignored and thus the related data are 
left as is
- sending an empty list means the related data should be cleared

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

PUT is supposed to replace the old instance by the new. Therefore, it is 
usually assumed that the previous related data will be cleared and that you’ll 
only have test after the request.

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

Usually, it’ll be the former.

Hopes this will help,

Regards,
Xavier,
Linovia.

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