After discussing the IRC in #tastypie
:
It is recommended that you do not change the standard behavior of the API, as this can be dangerous in the sense that clients will not see consistent behavior in the API.
One solution is to allow Tastypie to return a 4xx response when trying to create a Rating
, in which case the client will PATCH
existing rating.
If, however, a performance improvement is really necessary, you should only change the behavior if the client formally asks for it. Which in your case would mean adding the replace_existing_rating=True
parameter to the POST
request.
So, in your case, if you decide that you need to increase productivity, you can:
class CommentResource(ModelResource): def obj_create(self, bundle, request=None, **kwargs): if bundle.data.get("replace_existing_rating", False): try: bundle.obj = self._meta.object_class._default_manager.get(**conditions) except self._meta.object_class.DoesNotExist: bundle.obj = self._meta.object_class()
Prody source share