在django queryset中,是否已经有一种方法可以根据id % 4 == 0选择对象?
到目前为止,我找到的唯一解决方案是编写自定义转换,因为Postgresql似乎支持模块化,但我遇到了错误。
这是我的代码:
from django.db.models import Lookup, Transform
from django.db.models.fields import Field, IntegerField, AutoField
class Modulo4(Transform):
lookup_name = 'modulo4'
def as_sql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
return "%s %% 4" % lhs, params
AutoField.register_lookup(Modulo4)
foos = Foo.filter(id__modulo4=0)当对查询集进行评估时(使用foos.count()或其他方法),我会得到一个错误:
IndexError: tuple index out of range引用django文件。这似乎是由于模块,因为我设法使自定义转换与doc示例一起工作。
你知道怎么做到这一点吗?
https://stackoverflow.com/questions/30307421
复制相似问题