执行逻辑异或Django的Q对象(Perform a logical exclusive OR on

2019-08-16 18:28发布

我想执行逻辑异或( XOR上django.db.models.Q对象),使用操作模块的模型场的选择限制到外键的子集。 我在Django 1.4.3和Python 2.7.2一起做这个。 我有这样的事情:

import operator

from django.conf import settings
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User, Group

def query_group_lkup(group_name):
    return Q(user__user__groups__name__exact=group_name)

class Book(models.Model):
    author = models.ForeignKey(
                 User,
                 verbose_name=_("Author"),
                 null=False,
                 default='',
                 related_name="%(app_label)s_%(class)s_author",
                 # This would have provide an exclusive OR on the selected group name for User
                 limit_choices_to=reduce(
                     operator.xor,
                     map(query_group_lkup, getattr(settings, 'AUTHORIZED_AUTHORS', ''))
                 )

AUTHORIZED_AUTHORS是现有组名的列表。

但这并没有工作,因为Q对象不支持^操作符(只|和与来自运营商的文档 )。 从栈跟踪的信息是(部分地)执行以下操作:

File "/home/moi/.virtualenvs/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name, True)
  File "/home/moi/.virtualenvs/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/home/moi/.virtualenvs/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/opt/dvpt/toto/apps/book/models.py", line 42, in <module>
    class Book(models.Model):
  File "/opt/dvpt/toto/apps/book/models.py", line 100, in Book
    map(query_group_lkup, getattr(settings, 'AUTHORIZED_AUTHORS', ''))
TypeError: unsupported operand type(s) for ^: 'Q' and 'Q'

因此,受此启发答案 ,我试图实现我的特定查找的XOR。 这是不是真的灵活查找硬编码(我需要使用kwargs在query_xor的例如参数......)。 最后我做这样的事情:

from django.conf import settings
from django.db import models
from django.db.models import Q
from django.db.models.query import EmptyQuerySet
from django.contrib.auth.models import User, Group

def query_xor_group(names_group):
    """Get a XOR of the queries that match the group names in names_group."""

    if not len(names_group):
        return EmptyQuerySet()
    elif len(names_group) == 1:
        return Q(user__user__groups__name__exact=names_group[0])

    q_chain_or = Q(user__user__groups__name__exact=names_group[0])
    q_chain_and = Q(user__user__groups__name__exact=names_group[0])

    for name in names_group[1:]:
        query = Q(user__user__groups__name__exact=name)
        q_chain_or |= query
        q_chain_and &= query

    return q_chain_or & ~q_chain_and

class Book(models.Model):
    author = models.ForeignKey(
                 User,
                 verbose_name=_("author"),
                 null=False,
                 default='',
                 related_name="%(app_label)s_%(class)s_author",
                 # This provides an exclusive OR on the SELECT group name for User
                 limit_choices_to=query_xor_group(getattr(settings, 'AUTHORIZED_AUTHORS', ''))
                 )

它的工作原理,因为我想,但我在我看来,宁可不符合Python(特别是query_xor_group方法)。 会不会有这样做的更好(更直接的方式)?

基本上,我的问题可以被剥去limit_choices_to部分和可概括为:

我怎样才能让一个按位异或在Djangonic方式一组django.db.models.Q的对象?

Answer 1:

您可以添加一个__xor__()方法与Q使用和/或/不要做XOR逻辑。

from django.db.models import Q

class QQ:
    def __xor__(self, other):    
        not_self = self.clone()
        not_other = other.clone()
        not_self.negate()
        not_other.negate()

        x = self & not_other
        y = not_self & other

        return x | y

Q.__bases__ += (QQ, )

这样做之后,我才得以Q(...) ^ Q(...)filter()调用。

Foobar.objects.filter(Q(blah=1) ^ Q(bar=2)) 

这意味着原来的尝试不再抛出不支持的操作数异常。

limit_choices_to=reduce(
                     operator.xor,
                     map(query_group_lkup, getattr(settings, 'AUTHORIZED_AUTHORS', ''))
                 )

经测试,在Django 1.6.1Python 2.7.5



文章来源: Perform a logical exclusive OR on a Django Q object