Django 模型查询集过滤器作为函数
我正在尝试创建一个自定义函数,可以将其放置在查询集“链”中,该函数将对查询集应用过滤器。与普通的 Django 查询集过滤器一样,它将获取左侧的查询集并将结果查询集传递到右侧。
在将自定义函数添加到查询集链之前:
models.MyModel.objects.all()
models.MyModel.objects.some_manger_function()
models.MyModel.objects.some_manger_function().count()
models.MyModel.objects.some_manger_function().filter(title='something')
将自定义函数添加到查询集链之后:
models.MyModel.objects.all().my_custom_filter()
models.MyModel.objects.some_manger_function().my_custom_filter()
models.MyModel.objects.some_manger_function().my_custom_filter().count()
models.MyModel.objects.some_manger_function().my_custom_filter()\
.filter(title='something')
我不确定如何构造我的函数来执行此操作。它周围需要某种装饰器吗?
???? def my_custom_filter(???):
???? return qs.filter(id__gte=10)
有谁知道实现此目标的好方法?
下面的方法可能可行,但我希望有一些更像 Django 的东西。
def my_custom_filter(qs):
return qs.filter(id__gte=1)
my_custom_filter(models.MyModel.objects.all()).count()
非常感谢任何建议。
谢谢, Joe
更新:我正在尝试找出 Ignacio 解决方案的细节。我没有对 QuerySet 重写做太多事情,所以我正在拼凑我能找到的东西......
class MyQuerySet(QuerySet):
def filter(self, *args, **kwargs):
return super(self.__class__, self).filter(*args, **kwargs).\
filter(id__gt=5)
class MyManager(models.Manager):
def testqs(self):
return MyQuerySet(self.model)
但是,我认为这并没有按照我期望的方式工作。有什么建议吗?
>>> models.MyModel.objects.testqs().filter()
更新2: 事实证明这篇文章很有用。 http://zmsmith.com/2010/04/using-custom-django-querysets/
I'm trying to create a custom function that I can place in a queryset "chain" that will apply a filter to the queryset. Like with normal Django queryset filters, it will take the queryset to the left of it and pass the resulting queryset to the right.
Before adding my custom function to the queryset chain:
models.MyModel.objects.all()
models.MyModel.objects.some_manger_function()
models.MyModel.objects.some_manger_function().count()
models.MyModel.objects.some_manger_function().filter(title='something')
After adding my custom function to the queryset chain:
models.MyModel.objects.all().my_custom_filter()
models.MyModel.objects.some_manger_function().my_custom_filter()
models.MyModel.objects.some_manger_function().my_custom_filter().count()
models.MyModel.objects.some_manger_function().my_custom_filter()\
.filter(title='something')
I'm not sure how to construct my function to do this. Does it need some sort of decorator around it?
???? def my_custom_filter(???):
???? return qs.filter(id__gte=10)
Does anyone know a good way to accomplish this?
The following might work, but I was hoping for something a little more Django-like.
def my_custom_filter(qs):
return qs.filter(id__gte=1)
my_custom_filter(models.MyModel.objects.all()).count()
Any advice is much appreciated.
Thanks,
Joe
UPDATE: I'm trying to work out the details of Ignacio's solution. I've not done too much with QuerySet overriding so I'm piecing together what I'm able to find...
class MyQuerySet(QuerySet):
def filter(self, *args, **kwargs):
return super(self.__class__, self).filter(*args, **kwargs).\
filter(id__gt=5)
class MyManager(models.Manager):
def testqs(self):
return MyQuerySet(self.model)
However, I don't think this is working the way I expect. Any suggestions?
>>> models.MyModel.objects.testqs().filter()
UPDATE 2:
This article proved to be useful.
http://zmsmith.com/2010/04/using-custom-django-querysets/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要编写自己的
QuerySet
子类并添加方法,然后 在管理器中使用该类。You will need to write your own
QuerySet
child class with the method added, then use that class in the manager.