Friday, November 25, 2016

Re: Filtering in ListView

simplest way is update  get_content_data in view: 
 Take a look at example : 


class CompanyDetail(TemplateVariables, LoggedInMixin, DetailView):
    """ """
    model = Company
    template_name = 'company/company_detail.html'

    def cars(self):

        return Car.objects.filter(Company_id=context['company'].id)

    def get_context_data(self, **kwargs):
        """ filtering cars assigned to company with last coord"""
        context = super(CompanyDetail, self).get_context_data(**kwargs)
        CarList = Car.objects.filter(Company_id=context['company'].id) 
        PointList = []
        CarListFiltered = []
        for car in CarList:
            if Points.objects.filter(Car_id = car.id).exists():
                point = Points.objects.filter(Car_id = car.id).order_by('CreatedTime').last()
                PointList.append( point)
                CarListFiltered.append(car)
        context['Cars'] = CarListFiltered
        context['PointList'] = zip(CarListFiltered, PointList)
        return context




 This view returns  some additional data  but main idea is clear (line  with Car.object.filter)



Many thanks,

Serge


+380 636150445
skype: skhohlov

On Fri, Nov 25, 2016 at 10:38 AM, <jorrit787@gmail.com> wrote:
If you're ok with using an external package you can check out django-filter, it makes filtering very easy.


On Thursday, November 24, 2016 at 8:35:30 PM UTC+1, Artem Bernatskyy wrote:
Hello,

how can i accomplish filtering in ListView via GET ?

Now i am trying it with ListView and FormMixin...

To keep long story short:
- we are visiting page and filling form (which are generating from forms.py)
- than we are sending it via GET to the same page
- and somehow we need to validate income data and to filter by it


Any help is highly appreciated.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b704e15f-b422-4f31-a773-53f995946f7c%40googlegroups.com.

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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADTRxJOoOMQAOhCPe6%2BM5dgNTR3wXV6UGNGgiKpFsNiMAFqd5A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment