【问题标题】:How to filter results using forms in Django (best approach)如何在 Django 中使用表单过滤结果(最佳方法)
【发布时间】:2015-02-24 04:30:22
【问题描述】:

我是 python/django 的新手,我想知道:在 Django 中使用视图中的表单过滤结果的最佳方法是什么?

我需要根据 CandidateLook 模型按发色过滤候选人,根据 CandidateToJob 模型按状态过滤候选人。

但我收到一个错误:forms.py 中的语法无效: if self.cleaned_data['status']:

如果我删除这一行,forms.py 中的语法仍然无效: 返回job_candidates

提前致谢。

这是我的代码:

#models.py
class Job(models.Model):
    candidate = models.ManyToManyField('Candidate', through='CandidateToJob')
    title = models.CharField(max_length=500)
    ...

class Candidate(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    photo = models.ImageField(upload_to=user_profile_path)

class CandidateLook(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    haircolor = models.CharField(max_length=2, choices=HAIRCOLOR_CHOICES)

class CandidateToJob(models.Model):
    job = models.ForeignKey(Job, related_name='applied_to')
    candidate = models.ForeignKey(Candidate, related_name='from_user')
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
     )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES)

这是我的看法:

#views.py
class Screening(generic.DetailView):

model = Job
template_name = 'dashboard/screening.html'

    def get_context_data(self, **kwargs):
        context = super(Screening, self).get_context_data(**kwargs)

        # Fetch the sender_id for each unread message.
        # Count the number of times that the sender_id was included.
        # Then convert the list of tuples into a dictionary for quick lookup.
        sent_messages = dict(
            self.request.user.received_messages.filter(
                read_at__isnull=True
            ).values('sender_id').annotate(
                messages_sent=Count('sender_id')
            ).values_list('sender_id', 'messages_sent')
        )

        form_cand = ScreeningForm(self.request.GET)

        if form_cand.is_valid():
            job_candidates = form_cand.filter_job_candidates(self.object)
        else:
            job_candidates = self.object.applied_to.all().order_by('candidate')


        candidates = []
        for candidate in self.object.applied_to.all().order_by('candidate'):
            candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
            candidates.append(candidate)
        context['candidate_list'] = candidates
        context['form_cand'] = form_cand

        return context

表格

#forms.py
class StatusForm(ModelForm):
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)

    class Meta:
        model = CandidateToJob
        exclude = ['candidate', 'job']

class HairColorForm(ModelForm):
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=HAIRCOLOR_CHOICES)

    class Meta:
        model = CandidateLook

class ScreeningForm(forms.Form):
    haircolor = forms.ChoiceField(choices=CandidateLook.HAIRCOLOR_CHOICES, required=False)
    status = forms.ChoiceField(choices=CandidateToJob.STATUS_CHOICES, required=False)

    def filter_job_candidates(job):
        assert self.is_valid()

        job_candidates = job.applied_to.all().order_by('candidate')

        if self.cleaned_data['haircolor']:
            job_candidates = job_candidates.filter(candidate__candidatelook__haircolor=self.cleaned_data['haircolor']       

        if self.cleaned_data['status']:
            job_candidates = job_candidates.filter(status=self.cleaned_data['status']

        return job_candidates

还有模板:

#html
{{ job.title }}

{% for candidate in candidate_list %}

    {{ candidate }}

    {% for status in candidate.status %}

         {{ candidate.get_status_display }}

    {% endfor %}

{% endfor %}

【问题讨论】:

  • 嗨,andi,我已经尝试过 django-filter 和 django-datafilters,但不幸的是我无法实现我所需要的。我刚刚更新了我的帖子,因为我忘了展示我的表格。

标签: python django forms filter


【解决方案1】:

在朋友的帮助下解决了。

如果您需要 django 表单来过滤结果,我就是这样做的。

#models.py
class Job(models.Model):
    candidate = models.ManyToManyField('Candidate', through='CandidateToJob')
    title = models.CharField(max_length=500)
    ...

class Candidate(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    photo = models.ImageField(upload_to=user_profile_path)

class CandidateLook(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    haircolor = models.CharField(max_length=2, choices=HAIRCOLOR_CHOICES)

class CandidateToJob(models.Model):
    job = models.ForeignKey(Job, related_name='applied_to')
    candidate = models.ForeignKey(Candidate, related_name='from_user')
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
     )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES)

这是我的看法:

#views.py
class Screening(generic.DetailView):

model = Job
template_name = 'dashboard/screening.html'

    def get_context_data(self, **kwargs):
        context = super(Screening, self).get_context_data(**kwargs)

        # Fetch the sender_id for each unread message.
        # Count the number of times that the sender_id was included.
        # Then convert the list of tuples into a dictionary for quick lookup.
        sent_messages = dict(
            self.request.user.received_messages.filter(
                read_at__isnull=True
            ).values('sender_id').annotate(
                messages_sent=Count('sender_id')
            ).values_list('sender_id', 'messages_sent')
        )

        form_cand = ScreeningForm(self.request.GET)

        if form_cand.is_valid():
            job_candidates = form_cand.filter_job_candidates(self.object)
        else:
            job_candidates = self.object.applied_to.all().order_by('candidate')


        candidates = []
        for candidate in job_candidates:
            candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
            candidates.append(candidate)
        context['candidate_list'] = candidates
        context['form_cand'] = form_cand

        return context

表格

#forms.py
class StatusForm(ModelForm):
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)

    class Meta:
        model = CandidateToJob
        exclude = ['candidate', 'job']

class HairColorForm(ModelForm):
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=HAIRCOLOR_CHOICES)

    class Meta:
        model = CandidateLook

class ScreeningForm(forms.Form):
    haircolor = forms.ChoiceField(choices=CandidateLook.HAIRCOLOR_CHOICES, required=False)
    status = forms.ChoiceField(choices=CandidateToJob.STATUS_CHOICES, required=False)

    def filter_job_candidates(self, job):
        assert self.is_valid()

        job_candidates = job.applied_to.all().order_by('candidate')

        if self.cleaned_data['haircolor']:
            job_candidates = job_candidates.filter(candidate__candidatelook__haircolor=self.cleaned_data['haircolor'])      

        if self.cleaned_data['status']:
            job_candidates = job_candidates.filter(status=self.cleaned_data['status'])

        return job_candidates

还有模板:

#html
{{ job.title }}

<form method="get" action=".">
    {{ form_cand.as_p }}
    <input type="submit" value="Filtrar" />
</form>

{% for candidate in candidate_list %}

    {{ candidate }}

    {% for status in candidate.status %}

         {{ candidate.get_status_display }}

    {% endfor %}

{% endfor %}

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 2021-02-05
    • 2023-03-20
    相关资源
    最近更新 更多