简体   繁体   English

Django Deliciouspie通用关系

[英]Django Tastypie Generic Relation

I have some models like that in a Django project: 我在Django项目中有一些类似的模型:

class Link(BaseModel, BeginEndModel):
    entity0_content_type = models.ForeignKey(ContentType, related_name='link_from')
    entity0_object_id = models.PositiveIntegerField()
    entity0_content_object = generic.GenericForeignKey('entity0_content_type', 'entity0_object_id')

    entity1_content_type = models.ForeignKey(ContentType, related_name='link_to')
    entity1_object_id = models.PositiveIntegerField()
    entity1_content_object = generic.GenericForeignKey('entity1_content_type', 'entity1_object_id')

    link_type = models.ForeignKey(LinkType)

class Work(BaseModel, SluggedModel):
    """ Eser """
    name = models.CharField(max_length=255)
    links = generic.GenericRelation('Link', content_type_field='entity0_content_type', object_id_field='entity0_object_id')

I want to create a WorkResource with Tasypie Api like that: 我想像这样用Tasypie Api创建一个WorkResource:

from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie import fields, utils
from tastypie.contrib.contenttypes.fields import GenericForeignKeyField
from tastypie.authentication import Authentication, SessionAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from models import Link, LinkType, LinkPhrase
from models import Work

....

class WorkResource( BaseModelResource ):
    links = fields.ToManyField('musiclibrary.api.LinkResource', 'links_set')

    class Meta:
        queryset = Work.objects.all()
        always_return_data = True
        filtering = {
            'slug': ALL,
            'name': ['contains', 'exact']
        }

class LinkResource( ModelResource ):
    entity0_content_object = GenericForeignKeyField({
        Work: WorkResource,
        Artist: ArtistResource
    }, 'entity0_content_object')
    entity1_content_object = GenericForeignKeyField({
        Work: WorkResource,
        Artist: ArtistResource
    }, 'entity1_content_object')

    link_type = fields.ForeignKey(LinkTypeResource, 'link_type', full=True, null=True)

    class Meta:
        queryset = Link.objects.all()

When I want to try to see work resource result, links attribute is always an empty array. 当我想查看工作资源的结果时, links属性始终是一个空数组。 Why I couldn't make a relation between 2 resource? 为什么我不能在2种资源之间建立关系?

Note: I use Django 1.6.5, django-tastypie 0.11.1. 注意:我使用Django 1.6.5,django-tastypie 0.11.1。 I simplified my models.py and api.py samples above. 我在上面简化了我的models.py和api.py示例。 If needed I can share my full codes. 如果需要,我可以分享我的完整代码。

Its a bit tricky since there is 2 way relationship with ContentTypes flying around. 这有点棘手,因为与ContentTypes有两种联系。 I guess this would help : 我想这会有所帮助:

class WorkResource( BaseModelResource ):
    links = fields.ToManyField('musiclibrary.api.LinkResource', attribute=lambda bundle: Link.objects.filter(entity0_content_type=ContentType.objects.get_for_model(bundle.obj), entity0_object_id=bundle.obj.id))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM