Created
July 17, 2012 17:46
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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