> Le 13 août 2016 à 14:20, Jenna Pullen <[email protected]> a écrit :
>
> One further question regarding your response. If a PUT request should clear
> out the existing categories if a single category is posted. How would the
> user keep his existing catergories if he just wanted to add another category
> to the existing categories. Would they need to resend all the categories in
> the PUT request along with the new one? ie http .../api/questions PUT
> categories:=[all of them again even though he just wants to add a single
> category to the existing ones already associated]?
>
> Thank you
The workflow should be to issue a GET, alter the data and sent the PUT. So yes,
they should send all the related data.
This being said, you could choose an append only policy, but then you should
make it clear about the behavior and have an alternative way to clear the
related data.
Note that this is not the usual way users will expect the API to work.
Regards,
Xavier,
Linovia.
> On Saturday, 13 August 2016 12:16:45 UTC+2, Jenna Pullen wrote:
> 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]
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
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.