Django 1.8用户身份验证问题

时间:2015-04-08 07:43:22

标签: python django django-authentication django-1.8

当我尝试使用Django 1.8验证用户时:

authenticate(username=<username>, password=<password>)

我收到此错误:

lookup_cast() takes exactly 2 arguments (3 given)

回溯:

/lib/python2.7/site-packages/django/contrib/auth/__init__.pyc in authenticate(**credentials)
     72 
     73         try:
---> 74             user = backend.authenticate(**credentials)
     75         except PermissionDenied:
     76             # This backend says to stop in our tracks - this user should not be allowed in at all.
/lib/python2.7/site-packages/django/contrib/auth/backends.pyc in authenticate(self, username, password, **kwargs)
     15             username = kwargs.get(UserModel.USERNAME_FIELD)
     16         try:
---> 17             user = UserModel._default_manager.get_by_natural_key(username)
     18             if user.check_password(password):
     19                 return user
/lib/python2.7/site-packages/django/contrib/auth/models.pyc in get_by_natural_key(self, username)
    160 
    161     def get_by_natural_key(self, username):
--> 162         return self.get(**{self.model.USERNAME_FIELD: username})
    163 
    164 
/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
    125         def create_method(name, method):
    126             def manager_method(self, *args, **kwargs):
--> 127                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    128             manager_method.__name__ = method.__name__
    129             manager_method.__doc__ = method.__doc__
/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    326         if self.query.can_filter():
    327             clone = clone.order_by()
--> 328         num = len(clone)
    329         if num == 1:
    330             return clone._result_cache[0]
/lib/python2.7/site-packages/django/db/models/query.pyc in __len__(self)
    142 
    143     def __len__(self):
--> 144         self._fetch_all()
    145         return len(self._result_cache)
    146 
/lib/python2.7/site-packages/django/db/models/query.pyc in _fetch_all(self)
    963     def _fetch_all(self):
    964         if self._result_cache is None:
--> 965             self._result_cache = list(self.iterator())
    966         if self._prefetch_related_lookups and not self._prefetch_done:
    967             self._prefetch_related_objects()
/lib/python2.7/site-packages/django/db/models/query.pyc in iterator(self)
    236         # Execute the query. This will also fill compiler.select, klass_info,
    237         # and annotations.
--> 238         results = compiler.execute_sql()
    239         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
    240                                                   compiler.annotation_col_map)
/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    816             result_type = NO_RESULTS
    817         try:
--> 818             sql, params = self.as_sql()
    819             if not sql:
    820                 raise EmptyResultSet
/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in as_sql(self, with_limits, with_col_aliases, subquery)
    374             from_, f_params = self.get_from_clause()
    375 
--> 376             where, w_params = self.compile(self.query.where)
    377             having, h_params = self.compile(self.query.having)
    378             params = []
/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in compile(self, node, select_format)
    344             sql, params = vendor_impl(self, self.connection)
    345         else:
--> 346             sql, params = node.as_sql(self, self.connection)
    347         if select_format and not self.subquery:
    348             return node.output_field.select_format(self, sql, params)
/lib/python2.7/site-packages/django/db/models/sql/where.pyc in as_sql(self, compiler, connection)
    102             try:
    103                 if hasattr(child, 'as_sql'):
--> 104                     sql, params = compiler.compile(child)
    105                 else:
    106                     # A leaf node in the tree.
/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in compile(self, node, select_format)
    344             sql, params = vendor_impl(self, self.connection)
    345         else:
--> 346             sql, params = node.as_sql(self, self.connection)
    347         if select_format and not self.subquery:
    348             return node.output_field.select_format(self, sql, params)
/lib/python2.7/site-packages/django/db/models/lookups.pyc in as_sql(self, compiler, connection)
    203 
    204     def as_sql(self, compiler, connection):
--> 205         lhs_sql, params = self.process_lhs(compiler, connection)
    206         rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    207         params.extend(rhs_params)
/lib/python2.7/site-packages/django/db/models/lookups.pyc in process_lhs(self, compiler, connection, lhs)
    199         lhs_sql = connection.ops.field_cast_sql(
    200             db_type, field_internal_type) % lhs_sql
--> 201         lhs_sql = connection.ops.lookup_cast(self.lookup_name, field_internal_type) % lhs_sql
    202         return lhs_sql, params
    203 
TypeError: lookup_cast() takes exactly 2 arguments (3 given)

1 个答案:

答案 0 :(得分:1)

您使用的是哪个身份验证/数据库后端?

在Django 1.8中将lookup_cast()方法更改为:

def lookup_cast(self, lookup_type, internal_type=None)

从:

def lookup_cast(self, lookup_type)

如果您使用的是为1.7或更早版本设计的后端,则可能可以根据新方法签名调用它(3 args:self加2 args),但后端仍然需要2个args(self加1 ARG)。

也许您需要检查后端是否有可用的更新。