Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tastypie accessing fields from inherited models

Is it possible to include fields on related models, using tastypie?

As per my models below: if I persist one VideoContent and one TextContent instance to the DB, I can then get 2 objects back from my Content resource, however none of the additional fields are available.

Is it possible to include fields from related models (in this instance, the video url and the text content) and will that cater for adding more Content types in the future without having to rewrite the Content Resource, or am I coming at this from the wrong direction?

The goal is to be able to extend this with more ContentTypes without having to make changes to the Content resource (assuming it's possible to get it working in the first place)

Models.py:

class Content(models.Model):
    parent = models.ForeignKey('Content', related_name='children', null=True, blank=True)

class TextContent(Content):
    text = models.CharField(max_length=100)

class VideoContent(Content):
    url = models.CharField(max_length=1000)

And then my resources:

class ContentResource(ModelResource):
    children = fields.ToManyField('myapp.api.resources.ContentResource', 'children', null=True, full=True)

    class Meta:
        resource_name = 'content'
        queryset = ContentResource.objects.all()
        authorization = Authorization()
        always_return_data = True
like image 497
Jones Avatar asked Nov 04 '22 14:11

Jones


1 Answers

I found a good solution in another answer

Populating a tastypie resource for a multi-table inheritance Django model


I've run into the same problem - although I'm still in the middle of solving it. Two things that I've figured out so far:

django-model-utils provides an inheritence manager that lets you use the abstract base class to query it's table and can automatically downcast the query results.

One thing to look at is the dehydrate/rehydrate methods available to Resource classes.

This is what I did:

class CommandResource(ModelResource):

   class Meta:
        queryset = Command.objects.select_subclasses().all()

That only gets you half way - the resource must also include the dehydrate/rehydrate stuff because you have to manually package the object up for transmission (or recieving) from the user.

The thing I'm realizing now is that this is super hacky and there's gotta be a better/cleaner way provided by tastypie - they can't expect you to have to do this type of manual repackaging in these types of situations - but, maybe they do. I've only got about 8 hours of experience with tastypie @ this point so if I'm explaining this all wrong perhaps some nice stackoverflow user can set me straight. :D :D :D

like image 51
synthesizerpatel Avatar answered Nov 15 '22 07:11

synthesizerpatel