Django:表 minor_faculty 没有名为 user_id 的列

Posted

技术标签:

【中文标题】Django:表 minor_faculty 没有名为 user_id 的列【英文标题】:Django : table minor_faculty has no column named user_id 【发布时间】:2020-06-15 19:10:26 【问题描述】:

您好,我正在使用 sqlite3 数据库开发 Django 版本 2.2.4 应用程序。我有一个 Django 用户模型的扩展,在我的 models.py 中定义如下:


from django.db import models
from django.contrib.auth.models import User
DEPARTMENT = (
    ('CSE','Computer Science Engineering'),
    ('ECE','Electronics and Communication Engineering'),
    ('IT','Information Technology'),
    ('ME','Mechanical Engineering'),
    ('CE','Civil Engineering')
)

YEAR = (
    ('Third',3),
    ('Fourth',4)
)

SECTION = (
    ('1',1),
    ('2',2),
    ('3',3),
    ('4',4),
)


# Create your models here.
class Group(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    college = models.CharField(max_length = 300)
    department = models.CharField(choices = DEPARTMENT, max_length = 3)
    year = models.CharField(choices = YEAR, max_length = 5)
    section = models.CharField(choices = SECTION, max_length = 1)
    group_name = models.CharField( unique = True, max_length = 30)
    member_1_name = models.CharField( max_length = 100)
    member_2_name = models.CharField(max_length = 100)
    member_3_name = models.CharField(max_length = 100)
    member_1_enrollment = models.CharField(  max_length = 20)
    member_2_enrollment = models.CharField(max_length = 20)
    member_3_enrollment = models.CharField(max_length = 20)
    email = models.EmailField(max_length = 256)
    contact = models.PositiveIntegerField()
    mentor = models.ForeignKey('Faculty', on_delete = models.PROTECT)

    def __str__(self):
        return self.group_name

class Faculty(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    name = models.CharField( max_length = 100)
    email = models.EmailField(  max_length = 256)
    contact = models.PositiveIntegerField()
    college = models.CharField(  max_length = 300)
    department = models.CharField(choices = DEPARTMENT, max_length = 3)
    is_HOD = models.BooleanField(default = False)

    def __str__(self):
        return self.name

以下是我的views.py:


from django.shortcuts import render
from .models import Group, Faculty
from django.contrib.auth.models import User
# Create your views here.
from .forms import GroupInfoForm ,GroupUserForm, FacultyUserForm, FacultyInfoForm


def group_register(request):

    registered = False

    if request.method == 'POST':
        group_user_form = GroupUserForm(request.POST)
        group_form = GroupInfoForm(request.POST)

        if group_user_form.is_valid() and group_form.is_valid():
            user = group_user_form.save()
            user.set_password(user.password)
            user.save()

            group = group_form.save(commit = False) 
            group.user = user
            group.save()

            registered = True

        else:

            print(group_user_form.errors,group.errors)

    else:
        group_user_form = GroupUserForm()
        group_form = GroupInfoForm()

    return render(request,'register.html',
                            'registered': registered,
                            'group_user_form': group_user_form,
                            'group_form':group_form
    )

def faculty_register(request):

    registered = False

    if request.method == 'POST':
        faculty_user_form = FacultyUserForm(request.POST)
        faculty_form = FacultyInfoForm(request.POST)

        if faculty_user_form.is_valid() and faculty_form.is_valid():
            user = faculty_user_form.save()
            user.set_password(user.password)
            user.save()

            faculty = faculty_form.save(commit = False) 
            faculty.user = user
            faculty.save()

            registered = True

        else:

            print(faculty_user_form.errors,faculty_form.errors)

    else:
        faculty_user_form = FacultyUserForm()
        faculty_form = FacultyInfoForm()

    return render(request,'register_faculty.html',
                            'registered': registered,
                            'faculty_user_form': faculty_user_form,
                            'faculty_form':faculty_form
    )

每当尝试注册为教师时,我都会收到以下错误:

OperationalError at /faculty/

table minor_faculty has no column named user_id

我尝试了以下方法,但仍然遇到相同的错误:

python manage.py makemigrations <app name>
python manage.py migrate --run-syncdb

forms.py:

from django import forms 
from .models import Group,Faculty
from django.contrib.auth.models import User


class GroupUserForm(forms.ModelForm):
    password = forms.CharField(widget = forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username','email','password')

class GroupInfoForm(forms.ModelForm):
    class Meta:
        model = Group
        fields = ['college','department','year','section','group_name',
                  'member_1_name','member_1_enrollment','member_2_name','member_2_enrollment',
                  'member_3_name','member_3_enrollment','contact','mentor']


class FacultyUserForm(forms.ModelForm):
    password = forms.CharField(widget = forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username','email','password')

class FacultyInfoForm(forms.ModelForm):
    class Meta:
        model = Faculty
        fields = ['name','college','department','contact','is_HOD']

【问题讨论】:

错误与 minor_faculty 表有关,但我在您的 models.py 中没有看到 minor_faculty 模型 你能提供你的forms.py吗? 我添加了forms.py 【参考方案1】:

错误与minor_faculty 表有关,但我在您的models.py 中没有看到minor_faculty 模型。 如果您希望访问与 Faculty 模型关联的用户对象的 ID,您可以编写:

faculty_obj = Faculty.objects.get(id=1)
print(faculty_obj.user__id)

【讨论】:

以上是关于Django:表 minor_faculty 没有名为 user_id 的列的主要内容,如果未能解决你的问题,请参考以下文章

Django 删除了 DDBB 表,不能再迁移:没有这样的表错误

django 表没有名为 Exception 的列

有没有办法改变 django 中的添加表列?

django models建表设置default=1和blank=True没有效果

Django表单用户表单未登顶

Django IntegrityError:没有默认值