[Answered ]-How to hydrate ToOneField and ToManyField in tastypie

2👍

I can’t exactly answer what is right, but as far as I know, on Profile part, you will get problem when you create new object that doesn’t have profile linked yet.

However, what I regularly do is to only define if I want information or not.

class MinionResource(ModelResource):
    roles = fields.ToManyField(RoleResource, 'roles', full=True)
    profile = fields.ForeignKey(clouds_api.CloudProfileResource, 'profile', full=True)

This should be enough already. However, if you don’t like to be this way, you can do this.

class MinionResource(ModelResource):
    roles = fields.ListField()
    profile = fields.ForeignKey(clouds_api.CloudProfileResource, 'profile', full=True)

    def dehydrate_roles(self, bundle):
        return map(str, bundle.obj.roles.all())
👤sipp11

Leave a comment