I would like for a particular django-tastypie model resource to have only a subset of fields when listing objects, and all fields when showing a detail. Is this possible?
I would like for a particular django-tastypie model resource to have only a subset
Share
You would have to specify all fields in the actual ModelResource then override the
get_listmethod to filter out only the fields you want to show. See the internal implementation ofget_listonResourceto see how to override it.However, note this will only apply on GET requests, you should still be able to POST/PUT/PATCH on the resource with all fields if you authorization limits allow you to do so.
In a nut shell, you want to hot patch the internal field list before full_dehydrate is called on all ORM objects returned by
obj_get_list.Alternatively, you can let the full dehydrate mechanism take place and just at the end of it remove the fields you don’t want to show if you don’t care about squeezing out as much as speed as possible. Of course you would need to do this only if the URL is invoked as a consequence of get_list call. There is a convenience method for this
alter_list_data_to_serialize(request, to_be_serialized).Just do: