Привет. Я работаю над приложением Django версии 2.2.4, использующим базу данных sqlite3. У меня есть расширение для пользовательской модели 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']