Django中的筛选下拉菜单

时间:2019-01-14 07:28:20

标签: django django-models django-forms django-templates django-views

我想在用户选择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>

0 个答案:

没有答案