【问题标题】:django-tastypie ToOneFielddjango-tastypie ToOneField
【发布时间】:2013-08-06 07:03:14
【问题描述】:

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

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

这是我的模型和资源(sample_territory 始终为空,代码如下):

型号

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'

样本资源

class SampleResource(ProjectResource):

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

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

【问题讨论】:

    标签: django django-models tastypie


    【解决方案1】:

    显然我还不够了解tastepie 的属性字段选项。

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

    在我的情况下,解决方案是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-23
      • 2013-10-27
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2012-01-15
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多