Leszek Wroński
Leszek Wroński

Reputation: 369

Django: 'no such table' after extending the User model using OneToOneField

(Django 1.10.) I'm trying to follow this advice on extending the user model using OneToOneField. In my app 'polls' (yes, I'm extending the app made in the 'official' tutorial) I want to store two additional pieces of information about each user, namely, a string of characters and a number.

In my models.py I now have the following:

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    stopien = models.CharField(max_length=100)
    pensum = models.IntegerField()

and in admin.py the following:

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

from polls.models import Employee

class EmployeeInline(admin.StackedInline):
    model = Employee
    can_delete = False
    verbose_name_plural = 'employee'

class UserAdmin(BaseUserAdmin):
    inlines = (EmployeeInline, )

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

When adding a user using the admin panel my two new fields display correctly. However, when I click 'save', or if I don't add any user and just click on the name of my sole admin user in the admin panel, I get the following error:

OperationalError at /admin/auth/user/1/change/
no such table: polls_employee

I see some questions and answers related to similar problems, but they seem to be relevant for older version of Django. Could anyone give me a tip as to what I should do? Ideally I'd want my two additional fields display in the admin panel, though I suspect this might be a task for the future.

I have to confess I do not understand this paragraph from the documentation just following the advice I'm using:

These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.

Do I need to tie this 'post-save' to some element of the admin panel?

I'd be very greatful for any help!

Upvotes: 2

Views: 602

Answers (1)

Alasdair
Alasdair

Reputation: 309129

You need run makemigrations to create a migration for your new model, and then migrate to run the migration and create the database table.

./manage.py makemigrations
./manage.py migrate

Upvotes: 3

Related Questions