Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested resources in django tastypie

I have 2 models as below

merchant

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)

Category

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

I have resource file as below

Child resource

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
        }

Parent resource

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
        }

The output is

{"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}]

I need it as follows

{"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}]

The bottom line is "i want to call all related objects with single rest api, instead of calling seperate rest api, which need multiple request"

like image 398
hangman Avatar asked Oct 07 '22 20:10

hangman


1 Answers

You have to specify full = True as keyword arg in the category field declaration: http://django-tastypie.readthedocs.org/en/latest/fields.html#id1

like image 67
gcbirzan Avatar answered Oct 10 '22 02:10

gcbirzan