所以我有一个扩展Accounts
模型的AbstractUser
模型。
我还有StudentProfile
和TeacherApplications
与Accounts
的一对一关系。
我还有两个表格供老师和学生填写。
要求:让学生/教师通过表格注册。
问题 :在学生/教师表单中,我要求提供用户模型所需的字段,例如email, username, first and last name etc..
但是我得到的是:。< / p>
错误:1
/ register / Manager中的AttributeError不可用; 'auth.User'有 被换成'accounts.Accounts'
错误2
Student_profile.save() ^ TabError: inconsistent use of tabs and spaces in indentation
问题:我需要向用户注册用户字段,否则用户字段将为空并产生错误..但这是最好的方法吗?或者我做错了什么?
模型
class Accounts(AbstractUser):
email = models.EmailField('email address', unique=True)
first_name = models.CharField('first name', max_length=30, blank=True)
last_name = models.CharField('last name', max_length=30, blank=True)
date_joined = models.DateTimeField('date joined', auto_now_add=True)
# asdd
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
class StudentProfile(models.Model):
user = models.OneToOneField('Accounts', related_name='student_profile')
# additional fields for students
AMEB_Ratings = models.PositiveIntegerField(default=0)
is_student = models.BooleanField('student status', default=False)
class TeacherApplications(models.Model):
user = models.OneToOneField('Accounts', related_name='teacher_profile')
# additional fields for teachers
instrument = models.TextField(max_length=500, blank=True)
skill = models.CharField(max_length=30, blank=True)
experience_in_years = models.PositiveIntegerField(blank=True)
is_teacher = models.BooleanField('teacher status', default=False)
形式
class StudentResistrationForm(forms.ModelForm):
class Meta:
model = StudentProfile
fields = (
# 'username',
'first_name',
'last_name',
'email',
'date_joined',
# 'password1',
# 'password2',
'AMEB_Ratings',
'is_student',
)
def save(self, commit=True):
user = super(StudentResistrationForm, self).save(commit=False)
# user.first_name = self.cleaned_data['first_name']
# user.last_name = self.cleaned_data['last_name']
user.AMEB_Ratings = self.cleaned_data['AMEB_Ratings']
if commit:
user.save()
return user
class TeacherRegistrationForm(forms.ModelForm):
class Meta:
model = TeacherApplications
fields = (
'instrument',
'skill',
'experience_in_years',
'is_teacher',
)
视图
def registerStudent(request):
# Once register page loads, either it will send to the server POST data (if the form is submitted), else if it don't send post data create a user form to register
if request.method == "POST":
user_form = UserForm(request.POST)
form = StudentResistrationForm(request.POST)
if form.is_valid() and user_form.is_valid():
User = get_user_model()
username = user_form.cleaned_data['username']
email = user_form.cleaned_data['email']
email = user_form.cleaned_data['password']
new_user = User.objects.create_user(username=username, email=email, password=password)
Student_profile = StudentProfile()
Student_profile.user = new_user
Student_profile.AMEB_Ratings = request.POST['AMEB_Ratings']
# Student_profile = StudentProfile.create_user(AMEB_Ratings=AMEB_Ratings)
new_user.save()
Student_profile.save()
# form.save()
# AMEB_Ratings = form.cleaned_data['AMEB_Ratings']
return redirect('../home/')
else:
# Create the django default user form and send it as a dictionary in args to the reg_form.html page.
user_form = UserForm()
form = StudentResistrationForm()
args = {'form_student': form, 'user_form': user_form }
return render(request, 'accounts/reg_form_students.html', args)
def teacherApplication(request):
# # Once register page loads, either it will send to the server POST data (if the form is submitted), else if it don't send post data create a user form to register
# if request.method == "POST":
# form = TeacherRegistrationForm(request.POST)
# if form.is_valid():
# instrument = form.cleaned_data['instrument']
# skill = form.cleaned_data['skill']
# experience_in_years = form.cleaned_data['experience_in_years']
# is_teacher = form.cleaned_data['is_teacher']
# form.save()
# return redirect('../home/')
# else:
# # Create the django default user form and send it as a dictionary in args to the reg_form.html page.
# user_form = UserForm()
# form = StudentResistrationForm()
# return render(request, 'accounts/reg_form_teachers.html', {'form_student': form, 'user_form': user_form })
pass
答案 0 :(得分:0)
我认为您应该将模型更改为此类型,我的意思是Student
和Teacher
应该从帐户继承,因此您不再需要is_teacher
和is_student
。
class StudentProfile(Accounts):
AMEB_Ratings = models.PositiveIntegerField(default=0)
class TeacherApplications(Accounts):
instrument = models.TextField(max_length=500, blank=True)
skill = models.CharField(max_length=30, blank=True)
experience_in_years = models.PositiveIntegerField(blank=True)
对于您的第二个错误,我必须说问题是缩进,您应该用{4}替换tabs
。
另外,我需要您的urls.py
文件来完全弄清楚,我认为问题可能存在。
这个链接可能有用
Manager isn't available; User has been swapped for 'pet.Person'