我想在用户选择job_users
时为job_group
创建过滤的下拉选择菜单。这意味着,当用户选择一个组时,该组的用户将显示在job_users
中。我想过滤job_users
字段,因为它现在显示所有用户。
forms.py
class JobForm(forms.ModelForm):
class Meta:
model = Jobs
fields = ['job_name', 'job_group', 'job_users']
Jobs Model.py
class Jobs(models.Model):
job_name = models.CharField(max_length=30)
job_group = models.ForeignKey(Groups, on_delete=models.CASCADE)
job_users = models.ManyToManyField(User,related_name='user_job')
def __str__(self) :
return self.job_name
Views.py
class JobAddView(LoginRequiredMixin, generic.CreateView):
template_name = 'admin/jobs/jobs_form.html'
form_class = JobForm
success_url = '/useradmin/job/'
def form_valid(self, form):
return super().form_valid(form)
模板
<form class="forms-sample" action="" method="POST">
{% csrf_token %}
{{ form | crispy}}
</form>