Pergunta

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 ?

Foi útil?

Solução

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:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top