1

My project uses django.contrib.auth.models.User. I want to make email field unique, so I have defined CustomUser:

class CustomUser(AbstractBaseUser):
    username = models.CharField(_('username'), max_length=30, unique=True)
    email = models.EmailField(_('Email'), max_length=254, unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_staff = models.BooleanField(_('staff status'), default=False)
    is_active = models.BooleanField(_('active'), default=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

But when I run manage.py schemamigration myapp --auto, I get this error:

AttributeError: Manager isn't available; User has been swapped for 'myapp.models.CustomUser'

What have I done wrong?

niekas
  • 8,187
  • 7
  • 40
  • 58
  • 1
    Is this helpful - http://stackoverflow.com/questions/17873855/manager-isnt-available-user-has-been-swapped-for-pet-person – Mutant Jan 13 '14 at 21:34
  • related: http://stackoverflow.com/questions/15871164/custom-user-models-and-south – sk1p Jan 13 '14 at 21:38
  • @Mutant Didn't help. My settings.py contain ``AUTH_USER_MODEL = 'myapp.CustomUser'``. In models.py file I set foreign key to ``CustomUser``, in views.py and forms.py I use ``User = get_user_model()``. – niekas Jan 13 '14 at 21:50
  • @sk1p I tried adding ``class Meta: managed = False`` to ``CustomUser``, but nothing changed. – niekas Jan 13 '14 at 21:52
  • 1
    Did you run any migration before changing it to this custom migration? Some instruction from django documentation - https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model "If you intend to set AUTH_USER_MODEL, you should set it before creating any migrations or running manage.py migrate for the first time." – Mutant Jan 13 '14 at 22:18
  • @Mutant Yes, I already have 46 migrations without ``CustomUser``. It seems like I'll have to drop my migration history. – niekas Jan 13 '14 at 22:58
  • Yup. You will need to drop and re-run the migration. Let me know if it works, I'll move that to answer. – Mutant Jan 14 '14 at 01:45
  • @Mutant It doesn't. I have dropped the migrations. Even reset my local database. And no progress. I get the error even when accessing the shell (``manage.py shell``), the error is caused by statement in settings.py: ``AUTH_USER_MODEL = 'myapp.CustomUser'``. – niekas Jan 14 '14 at 01:53
  • @Mutant The problem was that I didn't replace ``User`` with ``get_user_model()`` in my django-rest-framework API. Your first comment led me to correct answer. – niekas Jan 14 '14 at 02:14
  • Awesome. Glad you able to resolve it. – Mutant Jan 14 '14 at 17:02

1 Answers1

0

As the answer suggests, I had to replace User with my new CustomUser everywhere in my code. Since I forgot to replace it in my django-rest-framework API code, South raised the error during schemamigration command.

niekas
  • 8,187
  • 7
  • 40
  • 58