ip.
ip.

Reputation: 3314

Limiting number of nested resources in Django Tastypie?

I'm trying to work out the following example. I just want to ask, how to you limit the number of results in the nested resources? How would I just get only the first 10 policies in the given example.

Thanks!

Upvotes: 2

Views: 606

Answers (1)

Vinicius Cainelli
Vinicius Cainelli

Reputation: 847

Hmm,

Instead of passing the attribute, you can pass a method that returns the queryset:

SchoolResource:

class SchoolResource(ModelResource):
    # fields.ToManyField('APP.api.RelatedResource', 'related name')
    policies = fields.ToManyField('places.api.PolicyResource', 
        attribute=lambda bundle: Policy.objects.filter(school=bundle.obj)[:10]
    )

    class Meta:
        resource_name = 'school'
        queryset = School.objects.all()
        allowed_methods = ['get']
        authorization = DjangoAuthorization()
        authentication = BasicAuthentication()

See this: http://django-tastypie.readthedocs.org/en/latest/fields.html#tomanyfield

I hope I was helpful =]

Upvotes: 3

Related Questions