3

I'm trying to extend the default Django's authentication model with additional fields and functionaliy, so I've decided to just go with extending User model and writing my own authentication backend.

from django.db import models from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User class MyUser(User): access_key = models.CharField(_('Acces Key'), max_length=64) 

This is really a basic code yet when trying to syncdb I'm cetting a strange error that Google doesn't know about :

CommandError: One or more models did not validate: core.sldcuser: '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.

In my settings.py I've added what I guess is required :

AUTH_USER_MODEL = 'core.MyUser' 

Had anyone stumbled upon this error ?

Or maybe I should use a one-to-one way, or a hybrid of 1t1 with proxy ?

3
  • what's the core determine here exactly it's a apps ? Commented Dec 6, 2013 at 4:08
  • shouldn't you be extending AbstractUser or AbstractBaseUser instead since these are the models with abstract = True? Commented Dec 6, 2013 at 4:19
  • I didn't even knew about those classes so not sure. Commented Dec 6, 2013 at 13:26

1 Answer 1

8

What you're doing right now, is creating a subclass of User, which is non-abstract. This means creating a table that has a ForeignKey called user_ptr pointing at the primary key on the auth.User table. However, what you're also doing by setting AUTH_USER_MODEL is telling django.contrib.auth not to create that table, because you'll be using MyUser instead. Django is understandably a little confused :P

What you need to do instead is inherit either from AbstractUser or AbstractBaseUser.

  • Use AbstractUser if you want everything that User has already, and just want to add more fields
  • Use AbstractBaseUser if you want to start from a clean state, and only inherit generic functions on the User, but implement your own fields.

REF:

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. Last time I've used Django few years back I guess there was no such thing as those Abstract models
Abstract models have been around since at least 1.0. I'm glad this helped you out though. Happy Hunting :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.