python - django tastypie 中的嵌套资源

标签 python django orm tastypie

我有如下两个模型

商家

class MerchantProfile(StateModel):

    class Meta:
        verbose_name = "Merchant Profile"
        ordering = ('name',)


    def __unicode__(self):
        return u'%s' % (self.name,)

    user = models.OneToOneField(UserProfile, related_name="merchant_profile")
    payment_card = models.OneToOneField(PaymentCard, related_name="merchant_profile")
    current_state = models.IntegerField('State', choices=STATE_CHOICES)
    name = models.CharField('Merchant Name', max_length=64)

类别

class Category(models.Model):
    merchant = models.ForeignKey(MerchantProfile, related_name="category")
    name = models.CharField(max_length=30)
    is_active=models.BooleanField()

我有如下资源文件

子资源

class MerchantCategoryResource(ModelResource):
    api_key = fields.CharField(attribute='merchant__user__api_key', readonly=True)
    class Meta:
        #get username from headers and apply filter query
        queryset = Category.objects.all()
        resource_name = 'merchantcategory'
        #excludes = ['id','email', 'password', 'is_active', 'is_staff', 'is_superuser']
        detail_allowed_methods = ['get']
        default_format = "application/json"
        filtering = {
            'user_id': ALL,
            'api_key':ALL
        }

父资源

class MerchantAllResource(ModelResource):
    category = fields.ToManyField(MerchantCategoryResource,'category')

    class Meta:
        #get username from headers and apply filter query
        queryset = MerchantProfile.objects.all()
        resource_name = 'merchantinfo'
        #excludes = ['id','email', 'password', 'is_active', 'is_staff', 'is_superuser']
        detail_allowed_methods = ['get']
        default_format = "application/json"
        filtering = {
            'user_id': ALL,
            'api_key':ALL
        }

输出是

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"category": ["/api/ecp/merchantcategory/1/"], "create_time": "2012-08-17T12:56:55", "current_state": 1, "id": 1, "modified_time": "2012-08-17T12:56:55", "name": "ram", "resource_uri": "/api/ecp/merchantinfo/1/", "utcStateCreated": null, "utcStateDisabled": null, "utcStateEnabled": null, "utcStateUnsubscribed": null}]

我需要如下

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"category": ["id": 1, "is_active": true, "name": "test1", "resource_uri": "/api/ecp/merchantcategory/1/"], "create_time": "2012-08-17T12:56:55", "current_state": 1, "id": 1, "modified_time": "2012-08-17T12:56:55", "name": "ram", "resource_uri": "/api/ecp/merchantinfo/1/", "utcStateCreated": null, "utcStateDisabled": null, "utcStateEnabled": null, "utcStateUnsubscribed": null}]

底线是“我想用单个 rest api 调用所有相关对象,而不是调用需要多个请求的单独 rest api”

最佳答案

您必须在类别字段声明中指定 full = True 作为关键字参数:http://django-tastypie.readthedocs.org/en/latest/fields.html#id1

关于python - django tastypie 中的嵌套资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12137870/

相关文章:

python-social-auth 和 Django,用自定义模型替换 UserSocialAuth

django - PyCharm manage.py运行服务器错误

django - 在 apache 上设置 django(mod_wsgi、virtualenv)

java - Hibernate 条件 - org.hibernate.QueryException : could not resolve property:

ios - DBAccess:获取任何一个属性值的数组

python - 如何在 Django 的 latex 模板中正确设置变量

python - 如何保存各个值并重新打开它们以供以后使用?

python - 如何在 ncurses 屏幕中输入单词?

.net - 哪个ORM BLToolkit不受欢迎?

Python CV2 视频编写器不保存视频