Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 207
FrancescoA Avatar asked Jun 25 '13 19:06

FrancescoA


1 Answers

Perhaps the error lies in incorrect zeroing of the database. 'Database error: no such table: str'

To delete the database, delete only database & all migrations in the migration packages(like 0001_initial.py and etc.).

Do not delete all migration packages and file "init.py". If you did this, create them. Django will not create them again. And after, do not remember run makemigrations & migrate.

like image 129
Anton Melnik Avatar answered Nov 06 '22 21:11

Anton Melnik