python–django tastypie中的嵌套资源

python–django tastypie中的嵌套资源,第1张

概述我有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,这需要多个请求”最佳答案您必须在类别字段声明中指定full = True作为关键字arg:http://django-tastypie.readthedocs.org/en/latest/fields.html#id1 总结

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

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1205509.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存