tags:

views:

92

answers:

1

My Url for pass to method:

customer_type=All&tag=2,3&city=8,9&last_contact=

is came from querydict = {customer_type:val1,tag:[], city:[],last_contact:valdate}

 def get_filter_result(self, customer_type='', tag_selected='', city_selected='', last_contact_filled=''):

            if customer_type != '':
                if customer_type =='All':
                    queryset = Customer.objects.filter(active=True).order_by('name')
                queryset = Customer.objects.filter(Q(type__name=customer_type),Q(active=True))


            if tag_selected != '':
                idtag = tag_selected.split(',')
                tag_qs = reduce(operator.or_, (Q(tag__id=x) for x in idtag))
                queryset = Customer.objects.filter(Q(type__name=customer_type),Q(active=True),
                                                   Q(tag_qs)
                                                   )
            if city_selected != '':
                idcity = city_selected.split(',')
                city_qs = reduce(operator.or_, (Q(city__id=x) for x in idcity))
                queryset = Customer.objects.filter(Q(type__name=customer_type),Q(active=True),
                                                   Q(tag_qs),
                                                   Q(city_qs)
                                                   )
            if last_contact_filled != '':
                last_contact_query = datetime.datetime.strptime(last_contact_filled,"%d/%m/%Y").strftime("%Y-%m-%d")
                queryset = Customer.objects.filter(Q(type__name=customer_type),Q(active=True),
                                                   Q(tag_qs),
                                                   Q(city_qs),
                                                   Q(last_contact=last_contact_query)
                                                   )

I have the problem to query in this case:(everybody Could help me to achieve this?

 customer_type=All&tag=2,3&city=8,9&last_contact= # Missing last_contact
 customer_type=All&tag=&city=8,9&last_contact= #Missing tag and last_contact
 customer_type=All&tag=2,3&city=&last_contact=2009-12-30 # missing city
 customer_type=All&tag=&city=&last_contact= #missing tag,city,last_contact
 .................

It the same

def get_filter_result(customer_type=None, tag_selected=None, city_selected=None, last_contact_filled=None):
    qdict = {}
    if customer_type is not None:
        qdict['type__name'] = customer_type
    if tag is not None:
        <repeat as appropriate>
    queryset = Customer.objects.filter(**qdict)# But my qdict it not only dictionary

or something like this

def get_filter_result(**kwargs):
    params = {}
    #delete items with empty strings
    for key in kwargs:
        if kwargs[key]:
            params[key] = kwargs[key]

    queryset = Customer.objects.filter(**params)
+1  A: 
def get_filter_result(request):
    queryset = Customer.objects.filter(active=True)
    customer_type = request.GET.get('customer_type','')
    if customer_type:
        queryset = queryset.filter(customer_type=customer_type)
    tags = request.GET.getlist('tag')
    if tags:
        queryset = queryset.filter(tag__id__in=tags)
    <...>
    return queryset.order_by('name')
Antony Hatchkins
Thank you very much .ahatchkinsBefore I tried like this instead of tag__id__in=tags:idtag = tag_selected.split(',') tag_qs = reduce(operator.or_, (Q(tag__id=x) for x in idtag))It is the same????
python
No, they are slightly different. tag__id__in will generate SELECT ... WHERE id IN (1,2,3) and Q|Q|Q will generate SELECT ... WHERE id=1 OR id=2 OR id=3
Antony Hatchkins