我需要username
在使用Django Auth框架时默认执行不区分大小写的查询.
我尝试通过编写自定义子类Queryset
并重写该_filter_or_exclude
方法来修复该问题,然后在User模型的自定义管理器中使用该子类 -
from django.db.models import Manager from django.db.models.query import QuerySet from django.contrib.auth.models import UserManager class MyQuerySet(QuerySet): def _filter_or_exclude(self, negate, *args, **kwargs): if 'username' in kwargs: kwargs['username__iexact'] = kwargs['username'] del kwargs['username'] return super(MyQuerySet, self)._filter_or_exclude(negate, *args, **kwargs) class MyUserManager(UserManager): def get_query_set(self): return MyQuerySet(self.model) User.objects = MyUserManager()
但是这种方法不起作用,我在尝试时遇到了一个奇怪的错误User.objects.get(username='Foo')
.
任何帮助,将不胜感激.
更新:我包含了我得到的确切错误.
/usr/lib/python2.5/site-packages/django/db/models/query.py in get(self, *args, **kwargs) 295 keyword arguments. 296 """ --> 297 clone = self.filter(*args, **kwargs) 298 num = len(clone) 299 if num == 1: /usr/lib/python2.5/site-packages/django/db/models/query.py in filter(self, *args, **kwargs) 481 set. 482 """ --> 483 return self._filter_or_exclude(False, *args, **kwargs) 484 485 def exclude(self, *args, **kwargs): /home/ghoseb/src/git/ocricket.git/ocricket/user/models.py in _filter_or_exclude(self, negate, *args, **kwargs) 38 kwargs['username__iexact'] = kwargs['username'] 39 del kwargs['username'] ---> 40 return super(MyQuerySet, self)._filter_or_exclude(negate, *args, **kwargs) 41 42 class MyUserManager(UserManager): /usr/lib/python2.5/site-packages/django/db/models/query.py in _filter_or_exclude(self, negate, *args, **kwargs) 499 clone.query.add_q(~Q(*args, **kwargs)) 500 else: --> 501 clone.query.add_q(Q(*args, **kwargs)) 502 return clone 503 /usr/lib/python2.5/django/db/models/sql/query.py in add_q(self, q_object, used_aliases) /usr/lib/python2.5/django/db/models/sql/query.py in add_filter(self, filter_expr, connector, negate, trim, can_reuse, process_extras) /usr/lib/python2.5/django/db/models/sql/query.py in get_meta(self): 'NoneType' object has no attribute '_meta'
更新:顺便说一句,我只是想提一下,当我将我的_filter_or_exclude
方法中的逻辑复制到实际的QuerySet
类中时,它可以完美地工作.
你不想搞乱Django类的内部功能.这种方式将来会遇到每次升级的麻烦.
如果要更改人员身份验证的方式,请编写自定义身份验证后端.
这是两个食谱.
http://www.davidcramer.net/code/224/logging-in-with-email-addresses-in-django.html
http://www.djangosnippets.org/snippets/577/
这两个人都是电子邮件而不是用户名.使用不区分大小写的查询而不是电子邮件查询并不难.