[英]Django save multiple user types
I am new to Django and would like to do a registration page for my web application. 我是Django新手,想为我的Web应用程序做一个注册页面。 Currently, I need two types of Users (Student and Instructor).
目前,我需要两种类型的用户(学生和教师)。 I have a checkbox for the user to select if they are student or instructor in my RegistrationForm.
我在我的RegistrationForm中有一个复选框供用户选择他们是学生还是教师。 I would like to know that under my RegistrationForm, how do I save them into Student or Instructor table respectively under the save() method?
我想知道在我的RegistrationForm下,如何将它们分别通过save()方法保存到Student或Instructor表中?
register.py register.py
class RegistrationForm(forms.ModelForm):
email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Email'}), label='')
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}), label='')
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password'}), label='')
INSTRUCTOR = "INS"
STUDENT = "STU"
roles_choices = [(INSTRUCTOR, "Instructor"), (STUDENT, "Student")]
role = forms.ChoiceField(choices=roles_choices, widget=forms.RadioSelect(), label='')
class Meta:
model = User
fields = ['email', 'password1', 'password2']
def clean(self):
"""
Verifies that the values entered into the password fields match
NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field.
"""
cleaned_data = super(RegistrationForm, self).clean()
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("Passwords don't match. Please enter both fields again.")
return self.cleaned_data
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
user.set_role(self.cleaned_data['role'])
if commit:
user.save()
return user
models.py models.py
class User(AbstractBaseUser):
role = models.CharField(max_length=3)
email = models.EmailField(max_length=100, primary_key=True)
first_name = models.CharField(max_length=30, null=False)
last_name = models.CharField(max_length=30, null=False)
password = models.CharField(max_length=100, null=False)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
class Meta:
abstract = True
def get_absolute_url(self):
return "/%s/%s/" % (self.role, urlquote(self.email))
def set_role(self, role):
self.role = role
def get_role(self):
return self.role
def get_short_name(self):
pass
def get_full_name(self):
pass
class Instructor(User):
pass
class Student(User):
matric_id = models.CharField(max_length=10, blank=False, null=False)
I'd have the Students and Instructors fields as a separate model with a user one to one field , with this you can even have the option to have the same user work as an Instructor and as a student if that works for your case. 我将“学生”和“讲师”字段作为一个单独的模型与用户一对一的字段来使用 ,您甚至可以选择与讲师和学生使用相同的用户工作(如果适用于您的情况)。
As for your form, you can have 3 forms view on the same page (one is for the regular registration and the other 2 differ according to what the user picks, instructor or student). 至于您的表格,您可以在同一页面上查看3个表格(一个用于常规注册,另外两个根据用户选择的内容(教师或学生)而不同)。
The saving part can be done in the views.py since you can extract 2 forms and save + link both instances. 保存部分可以在views.py中完成,因为您可以提取2个表单并保存并链接两个实例。
If you need more clarifications just ask and I'll gladly include some code snippets. 如果您需要更多说明,请询问,我将很乐意提供一些代码片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.