Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created July 17, 2012 17:46
Show Gist options
  • Save douglasmiranda/3130792 to your computer and use it in GitHub Desktop.
Save douglasmiranda/3130792 to your computer and use it in GitHub Desktop.
Implementando form de filtro numa ListView (Resposta para um tópico na Django Brasil)
# https://groups.google.com/forum/?fromgroups#!topic/django-brasil/d6gV1V9qrgU
from django.forms import ModelForm
from minha_app.models import MeuModel
class MeuForm(ModelForm):
class Meta:
model = MeuModel
# https://groups.google.com/forum/?fromgroups#!topic/django-brasil/d6gV1V9qrgU
from django.views.generic import ListView
from django.db.models import Q
from minha_app.forms import MeuForm
from minha_app.models import MeuModel
class MinhaListView(ListView):
model = MeuModel
template_name = 'meu_template.html'
context_object_name = 'resultados'
def get_queryset(self, **kwargs):
filtro1 = self.request.GET.get('parametro1', '')
filtro2 = self.request.GET.get('parametro2', '')
return MeuModel.objects.filter(Q(CAMPO1__icontains=filtro1) | Q(CAMPO2__icontains=filtro2))
def get_context_data(self, **kwargs):
context = super(MinhaListView, self).get_context_data(**kwargs)
context['meuform'] = MeuForm
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment