Take the 2-minute tour ×

I have a property in a Django Model that I'd like to expose via a TastyPie ModelResource.

My Model is

class UserProfile(models.Model):
    _genderChoices = ((u"M", u"Male"), (u"F", u"Female"))

    user = Models.OneToOneField(User, editable=False)
    gender = models.CharField(max_length=2, choices = _genderChoices)

    def _get_full_name(self):
        return "%s %s" % (self.user.first_name, self.user.last_name)

    fullName = property(_get_full_name)

My ModelResource is

class UserProfileResource(ModelResource):
    class Meta:
        queryset = models.UserProfile.objects.all()
        authorization = DjangoAuthorization()
        fields = ['gender', 'fullName']

However all I'm currently getting out of the tastypie api is:

    gender: 'female',
    resource_uri: "/api/v1/userprofile/55/"

I have tried playing with the fields property in the ModelResource, but that hasn't helped. Would love to understand what is going on here.

share|improve this question
don't miss the 'u' :return u"%s %s" % (self.user.first_name, self.user.last_name) –  dzen Feb 22 '13 at 13:01
add comment

2 Answers

up vote 22 down vote accepted

You should be able to define it as a field try:

class UserProfileResource(ModelResource):
    fullname = fields.CharField(attribute='_get_full_name', readonly=True)
    class Meta:
        queryset = models.UserProfile.objects.all()
        authorization = DjangoAuthorization()
        fields = ['gender',]


You also have to include: set readonly=True on your CharField, or TastyPie will try to set its value on insertion or update.

share|improve this answer
there is a way to order by this field? –  anacarolinats Mar 6 '13 at 21:02
add comment

A full example with dehydrate:

class UserResource(ModelResource):
    fullname = fields.CharField(readonly=True)

    class Meta:
        queryset = auth_models.User.objects.all()
        resource_name = 'user'

    def dehydrate_fullname(self, bundle):
        return u"{first_name} {last_name}".format(
            first_name=bundle.obj.first_name, last_name=bundle.obj.last_name)
share|improve this answer
add comment

Your Answer


