How to filter results using forms in Django/Python (best approach) ?

245 views
Skip to first unread message

Ronaldo Bahia

unread,
Feb 20, 2015, 3:10:28 PM2/20/15
to django...@googlegroups.com

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:

Vijay Khemlani

unread,
Feb 20, 2015, 9:33:34 PM2/20/15
to django...@googlegroups.com
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)

--
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.
For more options, visit https://groups.google.com/d/optout.

Ronaldo Bahia

unread,
Feb 21, 2015, 7:57:31 AM2/21/15
to django...@googlegroups.com
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.


Thanks

Vijay Khemlani

unread,
Feb 21, 2015, 5:49:21 PM2/21/15
to django...@googlegroups.com
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


Ronaldo Bahia

unread,
Feb 22, 2015, 5:12:51 PM2/22/15
to django...@googlegroups.com
Thanks. I will check this and let you know if it works. 
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/XfvhZjvQyEU/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Ronaldo Bahia

unread,
Feb 22, 2015, 8:53:41 PM2/22/15
to django...@googlegroups.com
Vijay,

For some reason, I'm getting SyntaxError with self.cleaned_data in forms.py 

I'd like you to teach me how to make this. I have no problem to share my project code.

Do you think we can make it?

Please, email me so we can discuss about it or add me in skype: bahiamartins

Thanks very much

Vijay Khemlani

unread,
Feb 22, 2015, 9:17:21 PM2/22/15
to django...@googlegroups.com
Well, I think you can continue posting the updated code here or pastebin or in the stackoverflow question, where other people can suggest solutions.

What is the complete message you are getting in your syntax error? can you upload your whole forms.py into pastebin or something?

Regards

Ronaldo Bahia

unread,
Feb 23, 2015, 5:41:06 AM2/23/15
to django...@googlegroups.com

Ronaldo Bahia 
Diretor | JobConvo.com
Tel: 11 3280 6971

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/XfvhZjvQyEU/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Vijay Khemlani

unread,
Feb 23, 2015, 8:50:49 AM2/23/15
to django...@googlegroups.com
Oh, if it is that line I missed a parenthesis in the my previuos snippet in the filter of haircolor

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

You also need to change the other filter

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

But you should ble able to detect that kind of errors, those are Python things, not something specifically about Django.




Ronaldo Bahia

unread,
Feb 23, 2015, 10:31:34 AM2/23/15
to django...@googlegroups.com
Oh, I'm feeling stupid right now :D

now my new error:

filter_job_candidates() takes exactly 1 argument (2 given)

in views.py

job_candidates = form_cand.filter_job_candidates(self.object)

I've tried to remove the arguments but django says "global name 'self' not defined"

I also think that in forms.py the correct way to write is:

def filter_job_candidates(job):
assert self.is_valid()
job_candidates = Job.objects.applied_to.all()

Or am I wrong?

Sorry for bothering...

Vijay Khemlani

unread,
Feb 23, 2015, 11:02:06 AM2/23/15
to django...@googlegroups.com
Change the declaration of the funciotn

def filter_job_candidates(job):

to

def filter_job_candidates(self, job):

And the filtering depends on the particular job being displayed on the view, if you change the line to

Job.objects.applied_to.all()

Then it would filter the candidates for all the jobs, not the particular one the user is seeing on the view (the one that is passed to the form method).




Ronaldo Bahia

unread,
Feb 23, 2015, 3:25:40 PM2/23/15
to django...@googlegroups.com
Awesome! Now it works perfectly.

I'm updating my stackoverflow code for anyone facing the same doubt.

Thanks a lot man!!!!

Vijay Khemlani

unread,
Feb 23, 2015, 7:33:32 PM2/23/15
to django...@googlegroups.com
You're welcome, glad I could help :D

C. Kirby

unread,
Feb 24, 2015, 10:57:05 AM2/24/15
to django...@googlegroups.com
I'm sorry I missed this thread at the beginning. I have an app that basically gives you the ability to build "advanced search" forms in almost the same way as model forms. You can take a look at it at https://github.com/ckirby/django-modelqueryform

If you try it or at least look at it, please let me know what you think. I am successfully using it in 3 different projects.

Best,
Kirby

Ronaldo Bahia

unread,
Feb 25, 2015, 6:15:01 PM2/25/15
to django...@googlegroups.com
Hi Kirby,

It seems very interesting. I'll let you know if I try it.
Just for curiosity, what differentiates your project from django-filters or django-datafilters ?

Thanks.

Ronaldo Bahia 

C. Kirby

unread,
Feb 25, 2015, 11:41:54 PM2/25/15
to django...@googlegroups.com
Great.
It is similar to django-filters. It looks like the maintainer of filters and I started our projects at about the same time. I think that my version provides more extension hooks out of the box, but also requires more development for filtering on string-based fields. I may build out on some of the ideas that django-filters uses for string fields.
Compared to django-datafiters, I think mine is more extensible and I have more documentation.

Kirby
Reply all
Reply to author
Forward
0 new messages