django-tastypie ToOneField

时间:2013-08-06 07:03:15

标签: django django-models tastypie

我似乎无法弄清楚如何让ToOneField在django-tastypie中工作。

>> pip freeze | grep django-tastypie
django-tastypie==0.10.0

这是我的模型和资源(使用以下代码,sample_territory始终为null):

模型

class Sample(ProjectModel):
    name = models.CharField(max_length=255)
    territory = models.OneToOneField(Territory, null=True, related_name='sample_territory')         

TerritoryResource

class TerritoryResource(ProjectResource):

    class Meta:
        queryset = Territory.objects.all()
        resource_name = 'sample_territory'

SampleResource

class SampleResource(ProjectResource):

    sample_territory = fields.ToOneField(TerritoryResource, 'sample_territory', related_name='sample_territory', null=True, full=True)

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

1 个答案:

答案 0 :(得分:0)

显然我不太了解tastypie的属性字段选项。

https://django-tastypie.readthedocs.org/en/latest/fields.html?highlight=toonefield#attribute

就我而言,解决方案是:

sample_territory = fields.ToOneField(TerritoryResource, attribute='territory', null=True, full=True)