您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – django tastypie中的嵌套资源

5b51 2022/1/14 8:20:56 python 字数 4244 阅读 462 来源 www.jb51.cc/python

我有2个型号如下商人class MerchantProfile(StateModel): class Meta: verbose_name = 'Merchant Profile' ordering = ('name',) def __unicode__(self): return u'%

概述

我有2个型号如下

商人

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','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,"objects": [{"category": ["id": 1,"is_active": true,"name": "test1","resource_uri": "/api/ecp/merchantcategory/1/"],"utcStateUnsubscribed": null}]

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

总结

以上是编程之家为你收集整理的python – django tastypie中的嵌套资源全部内容,希望文章能够帮你解决python – django tastypie中的嵌套资源所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶