Search code examples
djangotastypie

Less POST then GET fields in Django Tastypie


I have a ModelResource in Tastypie which exposes a set of fields which I whiltelisted like this: fields = ["fA", "fB", "fC"]. The model contains a couple of more fields. Now the question is, how can I have a detail GET method which contains these additional fields fD, fE and so on but restricting the POST method to the three original fields? Background is the following, the additional fields are for example something like this added = models.DateTimeField(auto_now_add=True) and I don't want my API users to have them submit those fields. Any suggestions are welcome.


Solution

  • It's as easy as to override the dehydrate method, e.g.:

    def dehydrate(self, bundle):
        bundle.data["fD"] = bundle.obj.fD
        bundle.data["custom_field"] = bundle.obj.custom_field
        return bundle
    

    See: http://django-tastypie.readthedocs.io/en/latest/cookbook.html#adding-custom-values