【问题标题】:Django-Tastypie Advanced FilteringDjango-Tastypie 高级过滤
【发布时间】:2013-06-15 11:55:59
【问题描述】:

我正在使用 Django-Tastypie。

我有一个类似

的网址
/api/v1/pic/?event=25&format=json

这将返回?event=25 的所有照片。但我还有其他一些事情需要考虑。

就像一个私人事件(即:event.private=1),它应该对返回的照片进行某种过滤。我该如何实施?任何指针都会有很大帮助。

【问题讨论】:

    标签: django rest tastypie


    【解决方案1】:

    你能说得更具体点吗? 你想要什么样的过滤器。

    对于私有事件过滤器,您可以在模型中定义一个布尔字段:

    ======================型号======================

    class Event(models.Model):
        private = models.BooleanField()
        ...
    
    class Pic(models.Model):
        event = models.ForeignKey(Event)
        date = models.DateTimeField()
        ...
    

    =====================资源=====================

    class PicResource(ModelResource):
        event = fields.ForeignKey(EventResource, 'event')
        class Meta:
            queryset = Pic.objects.all()
            filtering = {
                'event' : ALL_WITH_RELATIONS,
                'date' : ALL
            }
            ordering = ['date', 'event']
    

    然后,您可以查询资源:

    1. 私人活动中的所有图片 - /api/v1/pic/?event__private=True&format=json
    2. 所有图片按日期最新排序 - /api/v1/pic/?format=json&order_by=-date(注意“-”符号表示降序。

    【讨论】:

    • 如果事件是私有的(即:event.private=True),那么它应该检查所有具有(pic.private=1)的图片并仅返回那些图片。我怀疑这是否可以仅通过 URL 上的过滤器来完成,我想我需要在资源上编写一些逻辑,这样,如果 event(ex:event=25) 是私有的,则仅从该事件中检索私有照片。
    • 你是对的。您需要在资源端添加逻辑。但这可以使用tastepie的钩子来完成,例如“apply_filters” - django-tastypie.readthedocs.org/en/latest/…,或“build_filters” - django-tastypie.readthedocs.org/en/latest/resources.html#id5
    【解决方案2】:

    我来到这里是为了寻找更通用的 Tastypie 过滤,所以我想我会在 @ge7600 的答案中添加更多内容。

    如果你想过滤字段,你可以在 url 中使用任何有效的 Django 查询语法。如果我公开了这些字段进行过滤:

    class PicResource(ModelResource):
        event = fields.ForeignKey(EventResource, 'event')
        class Meta:
            queryset = Pic.objects.all()
            filtering = {
                'event' : ALL_WITH_RELATIONS,
                'date' : ALL,
                'title' : ALL
            }
            ordering = ['date', 'event']
    

    我可以使用以下 url 参数进行过滤:

    /api/v1/pic/?event=25
    /api/v1/pic/?title__contains=sunset
    /api/v1/pic/?date__gte=2015-06-01&title__contains=wedding
    

    如果我更仔细地阅读文档,我可能会更早发现这一点......

    【讨论】:

      【解决方案3】:

      您只需定义resource

      from django.contrib.auth.models import User
      from tastypie import fields
      from tastypie.authorization import DjangoAuthorization
      from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
      from myapp.models import Entry
      
      
      class UserResource(ModelResource):
          class Meta:
              queryset = User.objects.all()
              resource_name = 'auth/user'
              excludes = ['email', 'password', 'is_superuser']
      
      
      class EntryResource(ModelResource):
          user = fields.ForeignKey(UserResource, 'user')
      
          class Meta:
              queryset = Entry.objects.all()
              list_allowed_methods = ['get', 'post']
              detail_allowed_methods = ['get', 'post', 'put', 'delete']
              resource_name = 'myapp/entry'
              authorization = DjangoAuthorization()
              filtering = {
                  'slug': ALL,
                  'user': ALL_WITH_RELATIONS,
                  'created': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],
              }
      

      【讨论】:

        猜你喜欢
        • 2023-04-08
        • 1970-01-01
        • 2016-04-12
        • 2012-04-18
        • 2019-02-08
        • 2013-06-15
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        相关资源
        最近更新 更多