这个 (Django) CustomUser 字段从哪里来?
我的搜索仅出现有关如何添加字段
或add_fieldsets
in admin.py
时的提示。我需要找出名为admin
的字段来自何时在基于类的createView
中呈现模型表单时。代码没有错误,但是模板会在页面顶部自动添加admin
选择字段 - 下拉选择是已经创建的用户(所有3个类型)。但是我想创建一个新的员工
用户。那么,如何告诉django将admin
字段删除(至少直到保存用户)?
# models.py
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Staff"), (3, "Student"))
user_type = models.CharField(default=1, choices=user_type_data, max_length=10)
class Staff(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
email = models.EmailField()
objects = models.Manager()
# forms.py
class StaffForm(forms.ModelForm):
model = Staff
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class Meta:
model = Staff
fields = '__all__'
# views.py
class StaffCreateView(CreateView):
model = Staff
form_class = StaffForm
context_object_name = 'staff'
success_url = reverse_lazy('staff_list')
# staff_form.html
<form method="post">
{{ form.as_p }}
<input type="submit" value="Save">
</form>
My searches only turned up tips on how to add fields
or add_fieldsets
in admin.py
when using Django's CustomUser. I need to find out where a field named admin
comes from when the model form is rendered in the class-based CreateView
. There is no error with the code, but the template automatically adds an admin
choice field on top of the page-the drop-down choices are users (all 3 types) already created. But I want to create a new staff
user. So how do I tell Django to leave the admin
field out (at least until a user is saved)?
# models.py
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Staff"), (3, "Student"))
user_type = models.CharField(default=1, choices=user_type_data, max_length=10)
class Staff(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
email = models.EmailField()
objects = models.Manager()
# forms.py
class StaffForm(forms.ModelForm):
model = Staff
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class Meta:
model = Staff
fields = '__all__'
# views.py
class StaffCreateView(CreateView):
model = Staff
form_class = StaffForm
context_object_name = 'staff'
success_url = reverse_lazy('staff_list')
# staff_form.html
<form method="post">
{{ form.as_p }}
<input type="submit" value="Save">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的表格指定
fields ='__ all __'
,其中包括affer.admin
一对一字段。StaffForm.first_name
和staffform.last_name
覆盖这些模型字段的FormField,但并不是严格的原因是为什么表单上存在这些字段的原因。如果要从
scausform
中删除admin
字段,则应用fields ='__ all __'
用fields = ['first_name', 'last_name']
或dublude = ['admin']
。您可以在 django上了解更多信息。用于选择模型表单字段的文档。
Your form specifies
fields = '__all__'
, which includesStaff.admin
one-to-one field. TheStaffForm.first_name
andStaffForm.last_name
override the formfield for those model fields, but aren't strictly why those fields are present on the form.If you want to remove the
admin
field fromStaffForm
you should replacefields = '__all__'
withfields = ['first_name', 'last_name']
orexclude = ['admin']
.You can read more on the Django docs for selecting model form fields.