0

我正在尝试使用 django sweetpie,但遇到了一些麻烦。这是我的 api.py:

from tastypie.resources import ModelResource
from tastypie import fields
from app.models import First, Second, Third

class FirstResource(ModelResource):
    second = fields.ToManyField('app.api.SecondResource', 'second_set', related_name='first', null=True, blank=True, full=True)

    class Meta:
        queryset = First.objects.all()
        resource_name = 'first'

class SecondResource(ModeResource):
    first = fields.ForeignKey(FirstResource, 'first', full=True)
    third = fields.ToManyField('app.api.ThirdResource', 'third_set', related_name='second', null=True, blank=True, full=True)

    class Meta:
        queryset = Second.objects.all()
        resource_name = 'second'

class ThirdResource(ModelResource):
    poll = fields.ForeignKey(SecondResource, 'second', full=True)

    class Meta:
        queryset = Third.objects.all()
        resource_name = 'third'

当我尝试使用它获取 SecondResource 时,http://127.0.0.1:8000/app/api/v1/second/它​​包括 FirstResource,但我也希望与 ThirdResource 相关联。现在它只给了我一个空数组。我怎样才能做到这一点?

4

1 回答 1

0

尝试在您的第三个模型中添加相关名称:

class Third(models.Model):
    second = models.ForeginKey(Second, related_name=thirds)

然后在资源中:

   class SecondResource(ModeResource):`
       first = fields.ForeignKey(FirstResource, 'first', full=True)
       third = fields.ToManyField('app.api.ThirdResource', 'thirds', null=True, blank=True, full=True)
于 2013-02-23T20:47:52.597 回答