Skip to main content
 首页 » 编程设计

python之限制 Django Tastypie 中嵌套资源的数量

2024年08月30日27pander-it

我正在尝试计算以下 example .我只想问一下,如何限制嵌套资源中结果的数量?我如何只获得给定示例中的前 10 个策略。

谢谢!

请您参考如下方法:

嗯,

您可以传递一个返回查询集的方法,而不是传递属性:

学校资源:

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() 

看这个: http://django-tastypie.readthedocs.org/en/latest/fields.html#tomanyfield

希望对您有所帮助 =]