user2662705
user2662705

Reputation: 11

Django 1.5 custom user problems: "Cannot resolve keyword 'user' into field."

I've just converted a large django solution to 1.5's custom user model. Everything seems to have gone smoothly (the app starts up and allows me to log in) but I'm seeing strange behavior when trying a filter clause involving the new model.

In settings.py:

AUTH_USER_MODEL = 'acct.Account'

In models.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 

This works fine:

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

But any query that involves a filter or join fails:

>>> 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

What have I done wrong?

Upvotes: 0

Views: 1305

Answers (1)

user2662705
user2662705

Reputation: 11

Thanks for the suggestions. It turned out to be the ordering statement from the old profile model's meta, which started with "user__last_name". Ugh.

Upvotes: 1

Related Questions