0

I have a custom User model in Django 1.6:

from django.contrib.auth import models as usermodels

class User(usermodels.User):
    sid = models.CharField(max_length=20, unique=True)
    slug = models.SlugField(default="slug")

    objects = usermodels.UserManager()

In one of my templates, I have the following:

Welcome, {{ user.username }}: {{ user.sid }}

If the user is authenticated, this just displays as, for example, "Chris:" if the username is Chris. The custom field sid isn't shown! I've checked my tables and the user does have an sid stored. It seems that the template isn't getting my custom fields!

I've added my custom model as CUSTOM_USER_MODEL to my settings.

tao_oat
  • 1,011
  • 1
  • 15
  • 33
  • You shouldn't be inheriting from auth.User, but from auth.BaseUser. Doing it this way isn't your actual problem, but does introduce an unnecessary db call when fetching data. – Daniel Roseman Aug 16 '14 at 19:01
  • Do you mean `AbstractBaseUser` or `AbstractUser`? I can't find BaseUser anywhere in django.auth.models. – tao_oat Aug 16 '14 at 22:37
  • I extended `AbstractUser` instead of `User`, and set `AUTH_USER_MODEL` rather than `CUSTOM_USER_MODEL` in my settings. It worked! – tao_oat Aug 17 '14 at 13:01

1 Answers1

1

You should be setting your custom user model in the settings.py with AUTH_USER_MODEL as specified in the docs, not CUSTOM_USER_MODEL

crhodes
  • 1,178
  • 9
  • 20
  • This gives an error: `myapp.user: 'user_ptr' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.` I've looked around a bit but can't find a solution. – tao_oat Aug 16 '14 at 22:45
  • I'm using django.contrib.auth.get_user_model() to reference the user model, except in foreign keys where I'm using settings.AUTH_USER_MODEL as specified in the docs. – tao_oat Aug 16 '14 at 22:55
  • I'm not entirely sure where that error comes from, however maybe try sub-classing `AbstractUser` as mentioned in the [docs](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-django-s-default-user). It seems more appropriate for your use case. – crhodes Aug 16 '14 at 23:50
  • 1
    These two things together fixed the issue. Thank you! – tao_oat Aug 17 '14 at 13:01