If both forms are used in the same view, I see little reason to make them
separate and ModelForms, the form class I put above should be a better
choice.

In order to do the actual filtering, you can do something like this

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']:
            # You need to change the "user" field in CandidateLook to a
OneToOneField for this to work, which doesn't change anything since the
field is already a primary key that can't be repeated either way
            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

And in your view...

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 = ScreeningForm(self.request.GET)

        if form.is_valid():
            job_candidates = form.filter_job_candidates(self.object)
        else:
            # Fallback, since the form will always be valid unless someone
tampered with the form parameters manually
            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'] = form

        return context


On Sat, Feb 21, 2015 at 9:57 AM, Ronaldo Bahia <rona...@jobconvo.com> wrote:

> Hi Vijay,
>
> Thanks for your response.
> I've just updated my question in stackoverflow 'cause I forgot to show my
> forms.
>
> What I need is to enable a user (company) to filter results (candidates)
> based on forms selection (candidates' status and candidates' haircolors)
>
> And I don't know how to make this in my detailview.
>
> See the link:
> http://stackoverflow.com/questions/28637326/how-to-filter-results-using-forms-in-django-best-approach
>
> Thanks
>
> Em sábado, 21 de fevereiro de 2015 00:33:34 UTC-2, Vijay Khemlani escreveu:
>>
>> It's not clear what's the purpose of the view you are showing. ¿Is it
>> supposed to display a form with "haircolor" and "status" fields and filter
>> the results it displays based on that?
>>
>> In that case you can declare a form:
>>
>> from django import forms
>>
>> class ScreeningForm(forms.Form):
>>     haircolor = forms.ChoiceField(choices=CandidateLook.HAIRCOLOR_
>> CHOICES)
>>     status = forms.ChoiceField(choices=CandidateToJob.STATUS_CHOICES)
>>
>> then you can use it in your view, for example to display it:
>>
>> 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)
>>         context['form'] = ScreeningForm()
>>         return context
>>
>> HTML:
>>
>> <form method="get" action=".">
>>     {{ form.as_p }}
>>     <input type="submit" value="Filter" />
>> </form>
>>
>> (the code above just creates, displays and submits the form, it does not
>> filter your results yet)
>>
>> On Fri, Feb 20, 2015 at 5:10 PM, Ronaldo Bahia <ron...@jobconvo.com>
>> wrote:
>>
>>> I'm kind of new in python/django and I'd like to know: What's the best
>>> approach to filter results in Django using forms in a view?
>>>
>>> I need to filter candidates by haircolor, according to the model
>>> CandidateLook and by status, according to CandidateToJob model.
>>>
>>> Thanks in advance.
>>>
>>> Here is my code:
>>> http://stackoverflow.com/questions/28637326/how-to-
>>> filter-results-using-forms-in-django-best-approach
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/django-users/bf338512-fa18-4d97-a3e7-02588577e1a3%
>>> 40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/bf338512-fa18-4d97-a3e7-02588577e1a3%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6c2b451d-a98e-4dff-9cf9-3949d91dd435%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6c2b451d-a98e-4dff-9cf9-3949d91dd435%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei1Cu7ZTSXeiuqL_xdC0M9up2GUBE138fyzNvhWMaXd7EA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to