Search code examples
djangotastypie

Tastypie, field from an attribute or function?


For instance, I'd like to add absolute_url field when I return my model's json data.

I added the following to my model

@property
def absolute_url(self):
    return self.get_absolute_url()

then in my tastypie resource

absolute_url = fields.CharField(attribute="absolute_url", readonly=True)

class Meta:
    fields = [... 'absolute_url' .. ]

But this was not enough to make my resource to include the absolute_url data in the JSON response.

I had to use dehydrate to include the field.

The doc isn't clear about the effect of defining a field (here absolute_url CharField).


Solution

  • It turns out 'absolute_url' is rather special field in tastypie, and there's an even option for it.

    include_absolute_url