0

我刚刚将一个大型 django 解决方案转换为 1.5 的自定义用户模型。一切似乎都很顺利(应用程序启动并允许我登录),但在尝试涉及新模型的过滤器子句时,我看到了奇怪的行为。

在 settings.py 中:

AUTH_USER_MODEL = 'acct.Account'

在模型.py 中:

class AccountManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        now = timezone.now()
        if not email:
            raise ValueError('The email address must be set.')
        email = AccountManager.normalize_email(email)
        user = self.model(email=email,
                      is_staff=False, is_active=True, is_superuser=False,
                      last_login=now, date_joined=now, **extra_fields)

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password, **extra_fields):
        u = self.create_user(email, password, **extra_fields)
        u.is_staff = True
        u.is_active = True
        u.is_superuser = True
        u.save(using=self._db)
        return u

class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False)
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = 'email'

    objects = AccountManager()

    def get_full_name(self):
        return u' '.join((self.first_name, self.last_name))

    def get_short_name(self):
        return self.first_name

   ...more fields 

这工作正常:

>>> from ph.acct.models import Account
>>> a = Account.objects.get(id=1)
>>> a 
<Account: First Last Test User>

但是任何涉及过滤器或连接的查询都会失败:

>>> Account.objects.filter(first_name__icontains='first')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File ".../django/db/models/query.py", line 77, in __repr__ data = list(self[:REPR_OUTPUT_SIZE + 1])
File ".../django/db/models/query.py", line 92, in __len__ self._result_cache.extend(self._iter)
File ".../django/db/models/query.py", line 301, in iterator for row in compiler.results_iter():
File ".../django/db/models/sql/compiler.py", line 775, in results_iter for rows in self.execute_sql(MULTI):
File ".../django/db/models/sql/compiler.py", line 830, in execute_sql sql, params = self.as_sql()
File ".../django/db/models/sql/compiler.py", line 75, in as_sql ordering, ordering_group_by = self.get_ordering()
File ".../django/db/models/sql/compiler.py", line 394, in get_ordering self.query.model._meta, default_order=asc):
File ".../django/db/models/sql/compiler.py", line 420, in find_ordering_name field, col, alias, joins, opts = self._setup_joins(pieces, opts, alias)
File ".../django/db/models/sql/compiler.py", line 453, in  _setup_joins opts, alias, False)
File ".../django/db/models/sql/query.py", line 1332, in setup_joins 
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'user' into field. Choices are: ...all of the Account object's fields and relationships

我做错了什么?

4

1 回答 1

1

感谢您的建议。原来是来自旧配置文件模型元数据的排序语句,它以“user__last_name”开头。啊。

于 2013-08-08T14:20:07.227 回答