Filter with ChoiceField (Django)

2

Consider my form:

forms.py

status_list = (
    ('', ''),
    ('c', 'cancelado'),
    ('elab', 'em elaboração'),
    ('p', 'pendente'),
    ('co', 'concluido'),
    ('a', 'aprovado')
)

class StatusSearchForm(forms.Form):
    status = forms.ChoiceField(
        choices=status_list, widget=forms.Select(attrs={'class': 'form-control'}))

template

<form class="navbar-form navbar-right" action="." method="get">
    <!-- status search form -->
    <div class="form-group">
        <label>Status</label>
        {% for radio in status_search_form.status %}
            {{ radio }}
        {% endfor %}
    </div>
    <!-- search form -->
    <div class="form-group">
        <input id="search_box" name="search_box" type="text" placeholder="Localizar..." class="form-control">
        <button type="submit" class="btn btn-success form-control"><span class="glyphicon glyphicon-search"></span></button>
    </div>
</form>

views.py

class ProposalList(ListView):
    template_name = 'core/proposal/proposal_list.html'
    model = Proposal
    paginate_by = 10

    def get_context_data(self, **kwargs):
        context = super(ProposalList, self).get_context_data(**kwargs)
        context.update({'status_search_form': StatusSearchForm(), })
        return context

    def get_queryset(self):
        p = Proposal.objects.all().select_related()
        q = self.request.GET.get('search_box')
        if q is not None:
            try:
                p = p.filter(
                    Q(id__icontains=q) |
                    Q(work__name_work__icontains=q) |
                    Q(work__customer__first_name__icontains=q) |
                    Q(category__category__startswith=q) |
                    Q(employee__user__first_name__startswith=q) |
                    Q(seller__employee__user__first_name__startswith=q) |
                    Q(created__year=q))
            except ValueError:
                pass
        s = self.request.GET.get('status')
        if s is not None:
            p = p.filter(status__exact=s)
        elif s == '':
            p = p
        return p

Question : I wanted it when I chose the first 'status' option, which in case it is empty, it would return all records normally, the problem is that it is returning

http://localhost:8000/proposal/?status=&search_box=

And this does not return anything. But in this case I want to return everything.

What would be the best solution?

    
asked by anonymous 25.07.2015 / 06:46

1 answer

1

If you want it to return everything just check if the value sent is not empty ( q != '' ) and then do not perform the filtering.

p = Proposal.objects.all().select_related()
q = self.request.GET.get('search_box')
if not q in [None, '']:
    p = p.filter(
        Q(id__icontains=q) |
        Q(work__name_work__icontains=q) |
        Q(work__customer__first_name__icontains=q) |
        Q(category__category__startswith=q) |
        Q(employee__user__first_name__startswith=q) |
        Q(seller__employee__user__first_name__startswith=q) |
        Q(created__year=q)
    )
    
28.07.2015 / 15:34