Django 如何创建自己的扩展用户表单?
我有一个django模型,它扩展了用户:Django 如何创建自己的扩展用户表单?,django,forms,Django,Forms,我有一个django模型,它扩展了用户: class Student(models.Model): user = models.OneToOneField(User, unique=True) #other field in that profile #other field in that profile #other field in that profile 在settings.py中添加了: AUTH_PROFILE_MODULE = 'myapp.Studen
class Student(models.Model):
user = models.OneToOneField(User, unique=True)
#other field in that profile
#other field in that profile
#other field in that profile
在settings.py中添加了:
AUTH_PROFILE_MODULE = 'myapp.Student'
现在我想在我的网站上有一些表单来创建那个学生用户。最简单的方法是什么?
我不知道我应该在forms.py中创建ModelForm、forms.Form还是其他东西。
另外,我不知道如何在views.py文件中验证此表单。我只想添加具有此学生额外字段的新用户。
我仍在尝试一些方法,但没有任何效果!
请帮忙
我使用的是Django1.2.5,您很可能希望创建两个模型表单。 您可以在一个视图中处理这两个问题。只需先保存用户对象, 然后将该用户添加到从POST上的modelform创建的Student对象中
请查看该页面上的问题7以了解详细信息。您是否打算让您的用户通过管理站点访问此表单 如果是这样,那么结合这两个表单(用户和学生)的最简单解决方案就是在管理站点中使用内联模型 解决方案1(最简单-使用管理站点,): 现在,如果您不喜欢该解决方案,因为它看起来不漂亮,或者您不使用管理站点,那么您可以通过艰难的方式无缝地组合这两个表单(您不会看到它是两个不同的表单)。这个方法就是从这个角度衍生出来的 解决方案2(更高级的方法-无缝表单组合): 型号.py
class Student(models.Model):
user = models.OneToOneField(User, unique=True)
address = models.CharField(max_length=10)
# Create student instance on access - very useful if you plan to always have a Student obj associated with a User object anyway
User.student = property(lambda u: Student.objects.get_or_create(user=u)[0])
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
from testapp.myauth.models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
class UserForm(UserChangeForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
student_kwargs = kwargs.copy()
if kwargs.has_key('instance'):
self.student = kwargs['instance'].student
student_kwargs['instance'] = self.student
self.student_form = StudentForm(*args, **student_kwargs)
self.fields.update(self.student_form.fields)
self.initial.update(self.student_form.initial)
# define fields order if needed
self.fields.keyOrder = (
'last_name',
'first_name',
# etc
'address',
)
def clean(self):
cleaned_data = super(UserForm, self).clean()
self.errors.update(self.student_form.errors)
return cleaned_data
def save(self, commit=True):
self.student_form.save(commit)
return super(UserForm, self).save(commit)
forms.py
class Student(models.Model):
user = models.OneToOneField(User, unique=True)
address = models.CharField(max_length=10)
# Create student instance on access - very useful if you plan to always have a Student obj associated with a User object anyway
User.student = property(lambda u: Student.objects.get_or_create(user=u)[0])
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
from testapp.myauth.models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
class UserForm(UserChangeForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
student_kwargs = kwargs.copy()
if kwargs.has_key('instance'):
self.student = kwargs['instance'].student
student_kwargs['instance'] = self.student
self.student_form = StudentForm(*args, **student_kwargs)
self.fields.update(self.student_form.fields)
self.initial.update(self.student_form.initial)
# define fields order if needed
self.fields.keyOrder = (
'last_name',
'first_name',
# etc
'address',
)
def clean(self):
cleaned_data = super(UserForm, self).clean()
self.errors.update(self.student_form.errors)
return cleaned_data
def save(self, commit=True):
self.student_form.save(commit)
return super(UserForm, self).save(commit)
所以我在这里做的是在UserForm中创建StudentForm实例,并相应地组合它们的字段
<>我对你的唯一建议是考虑将你的个人资料模型重命名为更通用的,而不是学生(例如用户配置文件将工作),因为你永远不知道将来是否会有不同类型的用户,其他学生(如老师)。