Django:基于自定义函数过滤查询

发布于 2024-11-01 21:30:08 字数 428 浏览 4 评论 0原文

我的 Django 模型类中内置了一个函数,我想使用该函数来过滤查询结果。

  class service:
       ......
       def is_active(self):
            if datetime.now() > self.end_time:
                  return False
            return True

现在我想在我的查询过滤器中使用这个函数,就像

nserv = service.objects.filter(is_active=True)

我知道的那样,对于这个简单的“is_active”情况,我可以直接在过滤器查询中进行比较,但对于更复杂的情况,这可能是不可能的。我应该如何根据自定义函数进行查询?

I've got a function built into my Django model class and I want to use that function to filter my query results.

  class service:
       ......
       def is_active(self):
            if datetime.now() > self.end_time:
                  return False
            return True

Now I want to use this function into my query filter, something like

nserv = service.objects.filter(is_active=True)

I know, for this simple 'is_active' case, I can directly make this comparision in filter query, but for more complex situations, that may not be possible. How should I make a query, based on custom functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑色毁心梦 2024-11-08 21:30:08

我刚刚遇到了类似的问题。问题是我必须返回一个 QuerySet 实例。对我来说,一个快速的解决方案是做这样的事情:

active_serv_ids = [service.id for service in Service.objects.all() if service.is_active()]
nserv = Service.objects.filter(id__in=active_serv_ids)

我很确定这不是最漂亮和最高效的方法,但它对我有用。

更详细的方法是:

active_serv_ids = []

for service in Service.objects.all():
    if service.is_active():
        active_serv_ids.append(service.id)

nserv = Service.objects.filter(id__in=active_serv_ids)

I just had a similar issue. The problem was i had to return a QuerySet instance. A quick solution for me was to do something like this:

active_serv_ids = [service.id for service in Service.objects.all() if service.is_active()]
nserv = Service.objects.filter(id__in=active_serv_ids)

I'm pretty sure this is not the prettiest and performant way to do this, but it works for me.

A more verbose way of doing this would be:

active_serv_ids = []

for service in Service.objects.all():
    if service.is_active():
        active_serv_ids.append(service.id)

nserv = Service.objects.filter(id__in=active_serv_ids)
安人多梦 2024-11-08 21:30:08

我建议您为您的类使用自定义管理器,例如您可以使用:

nserv = service.objects.are_active()

这可以通过以下方式实现:< br>

class ServiceManager(models.Manager):
    def are_active(self):
        # use your method to filter results
        return you_custom_queryset

请参阅自定义管理器

I would suggest you to use a custom manager for your class, like this you could use :

nserv = service.objects.are_active()

This would be achieved with something like:

class ServiceManager(models.Manager):
    def are_active(self):
        # use your method to filter results
        return you_custom_queryset

See custom managers

江城子 2024-11-08 21:30:08

您可能无法,相反,您可以使用 列表理解对查询集进行后处理生成器表达式

例如:

[x for x in Q if x.somecond()]

You may not be able to, instead you can post-process the queryset with a list comprehension or generator expression.

For example:

[x for x in Q if x.somecond()]
坠似风落 2024-11-08 21:30:08

Ignacio 的答案很有趣,但它不返回查询集。这个的作用是:

def users_by_role(role):
    users = User.objects.all()
    ids = [user.id for user in users if user.role == role]
    return users.filter(id__in=ids)

The answer by Ignacio is interesting, but it does not return a queryset. This one does:

def users_by_role(role):
    users = User.objects.all()
    ids = [user.id for user in users if user.role == role]
    return users.filter(id__in=ids)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文