如何使用Django中的表单过滤结果(最佳方法)

时间:2015-02-20 20:06:23

标签: python django forms filter

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

根据CandidateLook模型和CandidateToJob模型的状态,我需要通过haircolor过滤候选者。

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

如果我删除此行,仍然会在forms.py中获取无效语法: return 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 %}

1 个答案:

答案 0 :(得分:0)

在朋友的帮助下解决。

如果您需要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 %}