How to load the foreign keys elements in Tastypie

一曲冷凌霜 提交于 2019-12-03 07:33:38

You'll want to create related resources for your foreign key fields and embed them in MyResource. If you make the embedded resource full=True, it'll dehydrate it when fetching MyResource, otherwise it'll embed it as the related resource uri.

class RelatedResource(ModelResource):
    class Meta:
        ...


class MyResource(ModelResource):
    related = fields.ForeignKey(RelatedResource, full=True)

    class Meta:
        ...

You can then filter by ?related__field=value in the GET request to MyResource.


If you're just wanting the field returned by the model's __unicode__, you can try doing the following (rather than embedding a related resource):

class MyResource(ModelResource):    
    city = fields.CharField(attribute="city")

    class Meta:
        ...

Where "city" is the field name of the foreign key on the MyData model.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!