我似乎无法弄清楚如何让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')
class TerritoryResource(ProjectResource):
class Meta:
queryset = Territory.objects.all()
resource_name = 'sample_territory'
class SampleResource(ProjectResource):
sample_territory = fields.ToOneField(TerritoryResource, 'sample_territory', related_name='sample_territory', null=True, full=True)
class Meta:
queryset = Sample.objects.all()
答案 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)