djangodjango-formsdjango-admindjango-authenticationdjango-syncdb

Database error: no such table: auth_user. Extending AbstractUser and using model to register on admin


I'm trying to use AbstractUser to add a field to Django's standard User model. This is my code:

class GUser(AbstractUser):
    uuid = UUIDField(auto=True)

This has been successful because from the shell I can say,

>>> a = GUser.objects.all()

>>> a
[<GUser: User1>]

>>> a[0].uuid
UUID('7d1f0b7b52144a2ea20db81ce74741e3')

The problem I am having is registering a new user from the /admin. When I create a new user I get a Database error:

no such table: auth_user

Before I get more into it, here is my forms.py:

class GUserChangeForm(UserChangeForm):
    class Meta:
        model = get_user_model()

class GUserCreationForm(UserCreationForm):

    class Meta:
        model = get_user_model()

    def clean_username(self):
            username = self.cleaned_data["username"]
            try:
                get_user_model().objects.get(username=username)
            except get_user_model().DoesNotExist:
                return username
            raise forms.ValidationError(self.error_messages['duplicate'])

And my admin.py:

class GUserAdmin(UserAdmin):
    form = GUserChangeForm
    add_form = GUserCreationForm
    fieldsets = (
        (None, {'fields': [('username', 'password')]}),
        ('Personal info', {'fields': ('is_active','is_staff','is_superuser','groups',    'user_permissions')}),
        ('Important dates', {'fields': ('last_login', 'date_joined')}),
        #('Universal ID', {'fields': ('uuid')})
        )

admin.site.register(GUser,GUserAdmin)

I read that when this has happened to other people they implemented their own Forms (as I did) to overwrite clean_username(self):

However I don't think this is getting called when I try to add a user from the admin. The auth.forms.py file is getting called even though I register my GUserAdmin.

Also, when I delete my database (sqlite3) and then call python manage.py syncdb, I see that a auth_user table is actually not getting created.

Yes, I have included AUTH_USER_MODEL in settings.py.

Any help would be greatly appreciated.

I have read: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms

Along with tons of SO posts but none of them seem to fix the issue I'm having.


Solution

  • So deleted all the work I had done with this and moved on with the standard user so that I could make progress. Then one day I went back to it and managed to get it working in an hour. I'll post the code for reference but I can't really pin point what made the difference. I do know that the creation forms and stuff were not necessary.

    models.py

    class PropaUser(AbstractUser):
        uuid = UUIDField(auto=True)
    

    admin.py

    from django.contrib import admin
    from login.models import PropaUser
    
    admin.site.register(PropaUser)
    

    settings.py

    AUTH_USER_MODEL = "login.PropaUser"
    

    other models.py

    class Word(models.Model):
        user = models.ManyToManyField(settings.AUTH_USER_MODEL)
        deck = models.ManyToManyField(Deck)
        word = models.CharField(max_length=100)
    
        def __unicode__(self):  # Python 3: def __str__(self):
        return self.word
    

    This is what is working. I do not know what was causing all the issues before because I think I tried this method before posting this question. This may not be a satisfactory answer but this is what is working. Earlier I was also confused about what it takes simply to add a field to the existing User. This is it.