简体   繁体   中英

Extending the existing User model in Django 1.7

I'm trying to extend the User model of a django app, but I keep getting the error:
OperationalError at /admin/auth/user/3/
Exception Value: no such column: subjects_subject.user_id

My Code:

#subjects/models.py
from django.contrib.auth.models import User
from django.db import models


class Subject(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100)

models.signals.post_save

#_admin/admin.py
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from .extended_admin import new_admin

from django.contrib import admin
from subjects.models import Subject

class SubjectInline(admin.StackedInline):
    model = Subject
    can_delete = False
    verbose_name_plural = 'subject'

class UserAdmin(UserAdmin):
    inlines = (SubjectInline, )

new_admin.register(User, UserAdmin)
new_admin.register(Group, GroupAdmin)

I have pretty much copied Django's own documentation word for word. Any help would really be appreciated!

EDIT: I also wanted to say that I have ran syndb and flush

I had the same issue and here are the steps I took to solve it. You did not specify the database you are using but in my case I am using MySQL. From Django docs here https://docs.djangoproject.com/en/1.8/topics/migrations/#mysql it seems sometimes creating the tables fails. I had a table called 'administration' and that kept failing to detect any changes.

Here is what I did:

  1. Remove migration directory from my app. This was so as to create new migration schemas
  2. Rename the directory. I renamed my directory from 'administration' to 'donor'. For some weird reason this seemed to fix the issue. I am not entirely sure why but could be 'administration' is a key word in either MySQL or Django
  3. If both steps do not work, you might have to manually add the changes as directed on the django docs. Even easier, you can drop the existing table if its empty and recreate it. ( WARNING! Please only do this if the table is empty otherwise you risk losing all your data )

All in all, there seems to be no outright reason as to why this could be happening at the moment. My trial and error approach seems to have fixed it.

Did you solved this problem?

I had the same issue , after different trying , I solved this. I'm using sqlite3 .

it seems like the when you first migrate , there is something wrong with the database table (I don't know what this problem caused by , so you raise the error : no such column: subjects_subject.user_id)

if you remove the migrations directory , and re-migrate , that will not solve this problem , because Django keeps track of all the applied migrations in django_migrations table.Migration was faked because the table already existed (probably with an outdated schema)SO I delete all the rows in the django_migrations table.

Here is what I did:

1.remove all files in migrations directory in my django app

2.using python manage.py dbshell , then DELETE FROM django_migrations WHERE app='your-app-name or DELETE FROM django_migrations WHERE ID='your-first-migrate

3.then python manage.py makemigrations python manage.py migrate

Successful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM