将查询集添加到 django updateview
Add Queryset to django updateview
我有一个更新视图,经理可以在其中编辑员工的所有字段。看起来像这样:(要求在更新视图的下拉列表中添加 associate_mgr)enter image description here
views.py
class ReallocationTeam(LoginRequiredMixin,UpdateView):
model = UserDetails
form_class = ViewEditSample
def get_success_url(self):
return reverse('UserProfile:index')
forms.py
class ViewEditSample(ModelForm):
class Meta:
model = UserDetails
fields = ['associate_name','client','lob','associate_mgr']
经理也应该能够编辑该员工的 "assciate_mgr"。
models.py
associate_name = models.CharField(max_length=200)
associate_nbr = models.CharField(max_length=8, primary_key=True)
associate_email = models.EmailField()
associate_department_id = models.CharField(max_length=50)
associate_mgr = models.CharField(max_length=100,blank=True, null=True)
associate_exec = models.CharField(max_length=100,blank=True, null=True)
associate_org = models.CharField(max_length=100,blank=True,null=True)
title = models.CharField(null=True, blank=True, max_length=100)
date_of_service = models.CharField(null=True,blank=True,max_length=11)
is_manager = models.BooleanField(default=False)
is_exec = models.BooleanField(default=False)
is_team_lead = models.BooleanField(default=False)
但 associate_mgr 不是我的数据库中的选择字段。
我需要在我的 UpdateView 中添加一个包含 associate_mgr 的下拉列表。我该如何着手实施?
我是否应该着手编写查询以获取所有经理并在下拉列表中填充他们:像这样 mgr = UserDetails.objects.filter(is_manager=True) 但是我如何将所选内容存储在 associate_mgr 字段中在分贝?
您可以将 ModelForm
中的表单字段覆盖为带有选项列表的 ChoiceField
:UserDetails.objects.filter(is_manager=True).values_list('name')
.
associate_mgr = forms.ChoiceField(choices=
UserDetails.objects.filter(is_manager=True).values_list('associate_name', 'associate_name')
)
然后选择将自动保存('associate_name' 字段值)。
但在您的模型上使用 ForeignKey
可能比 CharField
更好。这会将值强制为其他 UserDetails
而不仅仅是字符串。
我有一个更新视图,经理可以在其中编辑员工的所有字段。看起来像这样:(要求在更新视图的下拉列表中添加 associate_mgr)enter image description here
views.py
class ReallocationTeam(LoginRequiredMixin,UpdateView):
model = UserDetails
form_class = ViewEditSample
def get_success_url(self):
return reverse('UserProfile:index')
forms.py
class ViewEditSample(ModelForm):
class Meta:
model = UserDetails
fields = ['associate_name','client','lob','associate_mgr']
经理也应该能够编辑该员工的 "assciate_mgr"。 models.py
associate_name = models.CharField(max_length=200)
associate_nbr = models.CharField(max_length=8, primary_key=True)
associate_email = models.EmailField()
associate_department_id = models.CharField(max_length=50)
associate_mgr = models.CharField(max_length=100,blank=True, null=True)
associate_exec = models.CharField(max_length=100,blank=True, null=True)
associate_org = models.CharField(max_length=100,blank=True,null=True)
title = models.CharField(null=True, blank=True, max_length=100)
date_of_service = models.CharField(null=True,blank=True,max_length=11)
is_manager = models.BooleanField(default=False)
is_exec = models.BooleanField(default=False)
is_team_lead = models.BooleanField(default=False)
但 associate_mgr 不是我的数据库中的选择字段。 我需要在我的 UpdateView 中添加一个包含 associate_mgr 的下拉列表。我该如何着手实施? 我是否应该着手编写查询以获取所有经理并在下拉列表中填充他们:像这样 mgr = UserDetails.objects.filter(is_manager=True) 但是我如何将所选内容存储在 associate_mgr 字段中在分贝?
您可以将 ModelForm
中的表单字段覆盖为带有选项列表的 ChoiceField
:UserDetails.objects.filter(is_manager=True).values_list('name')
.
associate_mgr = forms.ChoiceField(choices=
UserDetails.objects.filter(is_manager=True).values_list('associate_name', 'associate_name')
)
然后选择将自动保存('associate_name' 字段值)。
但在您的模型上使用 ForeignKey
可能比 CharField
更好。这会将值强制为其他 UserDetails
而不仅仅是字符串。