在相关模型中显示 auth.User 个字段
Display auth.User fields in related model
我创建了这个 Django 模型:
class Teacher(models.Model):
user = models.OneToOneField('auth.User', on_delete=models.CASCADE, default=1)
surname = models.CharField(max_length=15, null=True)
creation_date = models.DateTimeField('creation date', auto_now_add=True)
deletion_date = models.DateTimeField('deletion date', null=True, blank=True)
以及以只读模式表示它的表单:
class TeacherViewForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TeacherViewForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.fields:
self.fields[field].widget.attrs['disabled'] = True
class Meta:
model = Teacher
exclude = ['deletion_date']
视图如下所示:
class TeacherViewDetail(generic.DetailView):
model = Teacher
template_name = 'control/teacher_detail.html'
form_class = TeacherViewForm
def get_form(self):
# Instantiate the form
form = self.form_class(instance=self.object)
return form
def get_context_data(self, **kwargs):
context = super(TeacherViewDetail, self).get_context_data(**kwargs)
context.update({
'form': self.get_form(),
})
return context
如您所见,Teacher
和 auth.User
模型之间存在 OneToOne
关系。我需要显示 auth.User
型号的 first_name
和 last_name
,但只显示 username
。
如何以显示字段 Teacher.surname
的相同方式显示这些字段?
我是否必须将它们包含在模型中,form.fields 或者是否有一个 属性 我必须修改才能实现它?
谢谢
您必须删除 user
字段并添加适当的字段。然后在你的表单的 __init__
中,填写初始值:
# forms.py
class TeacherViewForm(ModelForm):
first_name = forms.CharField()
last_name = forms.CharField()
def __init__(self, *args, **kwargs):
super(TeacherViewForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.fields:
self.fields[field].widget.attrs['disabled'] = True
# set initial values
self.fields['first_name'].initial = instance.user.first_name
self.fields['last_name'].initial = instance.user.last_name
class Meta:
model = Teacher
exclude = ['user', 'deletion_date'] # exclude user
但是,如果禁用所有字段,为什么还要使用表单?您可以在模板中更自由地呈现所有这些信息。
我创建了这个 Django 模型:
class Teacher(models.Model):
user = models.OneToOneField('auth.User', on_delete=models.CASCADE, default=1)
surname = models.CharField(max_length=15, null=True)
creation_date = models.DateTimeField('creation date', auto_now_add=True)
deletion_date = models.DateTimeField('deletion date', null=True, blank=True)
以及以只读模式表示它的表单:
class TeacherViewForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TeacherViewForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.fields:
self.fields[field].widget.attrs['disabled'] = True
class Meta:
model = Teacher
exclude = ['deletion_date']
视图如下所示:
class TeacherViewDetail(generic.DetailView):
model = Teacher
template_name = 'control/teacher_detail.html'
form_class = TeacherViewForm
def get_form(self):
# Instantiate the form
form = self.form_class(instance=self.object)
return form
def get_context_data(self, **kwargs):
context = super(TeacherViewDetail, self).get_context_data(**kwargs)
context.update({
'form': self.get_form(),
})
return context
如您所见,Teacher
和 auth.User
模型之间存在 OneToOne
关系。我需要显示 auth.User
型号的 first_name
和 last_name
,但只显示 username
。
如何以显示字段 Teacher.surname
的相同方式显示这些字段?
我是否必须将它们包含在模型中,form.fields 或者是否有一个 属性 我必须修改才能实现它?
谢谢
您必须删除 user
字段并添加适当的字段。然后在你的表单的 __init__
中,填写初始值:
# forms.py
class TeacherViewForm(ModelForm):
first_name = forms.CharField()
last_name = forms.CharField()
def __init__(self, *args, **kwargs):
super(TeacherViewForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
for field in self.fields:
self.fields[field].widget.attrs['disabled'] = True
# set initial values
self.fields['first_name'].initial = instance.user.first_name
self.fields['last_name'].initial = instance.user.last_name
class Meta:
model = Teacher
exclude = ['user', 'deletion_date'] # exclude user
但是,如果禁用所有字段,为什么还要使用表单?您可以在模板中更自由地呈现所有这些信息。