Tastypie从继承的模型访问字段 [英] Tastypie accessing fields from inherited models

查看:104
本文介绍了Tastypie从继承的模型访问字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



根据以下我的模型:如果我将一个VideoContent和一个TextContent实例保留到数据库,然后我可以从我的内容资源中获取2个对象,但是没有其他字段可用。



是否可以包含相关模型的字段(在这种情况下,视频网址和文字内容),以及将来会否增加更多内容类型,而无需重写内容资源,或者我是从错误的方向出发?



目标是能够使用更多的ContentTypes来扩展这个内容,而无需对Content资源进行更改(假设可以首先将其更改)



Models.py:

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

class TextContent(Conten t
text = models.CharField(max_length = 100)

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

然后我的资源:

  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


解决方案

我在另一个答案中找到了一个很好的解决方案



填充多表继承的tastypie资源Django模型






我遇到同样的问题 - 虽然我'仍然在解决它的中间。到目前为止我已经解决了两件事情:



django-model-utils提供了一个继承管理器,可让您使用抽象基类来查询它的表,并可以自动downcast查询结果。



需要注意的是脱水/再水合可用于资源类的方法。



这就是我所做的:

  class CommandResource(ModelResource):

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

只有你半途而废 - 资源还必须包括脱水/必须手动将对象打包,以便从用户传输(或接收)。



我现在意识到的是,这是超级黑客,必须是一个更好/更清洁的方式提供的口味 - 他们不能指望你必须做这种类型的手动重新包装在这些类型的情况 - 但也许他们这样做。我只有大约8个小时的口味@这个点的经​​验,所以如果我解释这一切都错了也许一些很好的stackoverflow用户可以让我直线。 :D:D:D


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

解决方案

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

这篇关于Tastypie从继承的模型访问字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆