开发者

intelligent methodology for filtering server side

My lack of CS and inexperience is really coming to the forefront in this moment. I've never really handled filtering results server side. I'm thinking that this is not the right way to go about it. I'm using Django....

First, I assume that I can keep it DRYer by keeping this validation in my form definitions. Next, I was concerned about my chained filter statements. How important is it to use Q complex lookups as opposed to chaining filters at this point? I'm just building a prototype and I assume that I'll eventually have to go for a search solution more powerful than full text search.

My big issue right now (besides the length of the code and clearly the inefficiency) is that I'm not sure how to handle my rooms and workers inputs, which are select forms. If the user does not select a value, I want to remove these filters from the process server side. Should I just create two separate conditional series of lookups for these outcomes?

def search(request):
   if request.method=='GET' and request.GET.get('region',''):
           neighborhoods=request.GET.getlist('region')
       min_rent=request.GET.get('min_cost','0')
       min_rent=re.sub(r'[,]','',min_cost)  #remove any ','
       if re.search(r'[^\d]',min_cost):
           min_cost=0
       else:
           min_cost=int(min_cost)

       max_cost=request.GET.get('max_cost','0')
       max_cost=re.sub(r'[,]','',max_cost)  #remove any ','
       if re.search(r'[^\d]',max_cost):
           max_cost=100000
       else:
           max_cost=int(max_rent)   


       date_min=request.GET.get('from','')  
       date_max=request.GET.get('to','')
       if not date_min:
           date=(str(datetime.date.today()))
           date_min=u'%s' %date
       if not date_max:
           date_max=u'2013-03-18'

               rooms=request.GET.get('rooms',0)
   开发者_开发技巧    if not rooms:
        rooms=0

       workers=request.GET.get('workers',0)
       if not workers:
           workers=0

    #I should probably use Q objects here for complex lookups
    posts=Post.objects.filter(region__in=region).filter(cost__gt=min_cost).filter(cost__lt=max_cost).filter(availability__gt=date_min).filter(availability__lt=date_max).filter(rooms=rooms).filter(workers=workers)
    #return HttpResponse('%s' %posts)

    return render_to_response("website/search.html",{'posts':posts),context_instance=RequestContext(request))   


First, I assume that I can keep it DRYer by keeping this validation in my form definitions.

Yes, I'd put this in a form as it looks like you are using one to display the form anyways? Also, you can put a lot of your date formatting stuff right in the clean_FIELD methods to format the data in the cleaned_data dict. The only issue here is that output is actually modified so your users will see the change from 1,000 to 1000. Either way, I would put this logic in a form method.

# makes the view clean.
if form.is_valid():
    form.get_posts(request)
    return response

My big issue right now (besides the length of the code and clearly the inefficiency) is that I'm not sure how to handle my rooms and workers inputs, which are select forms. If the user does not select a value, I want to remove these filters from the process server side. Should I just create two separate conditional series of lookups for these outcomes?

Q objects are only for complex lookups. I don't see a need for them here.

I also don't see why you need to chain the filters. I at first wondered if these are m2m, but these types of queries (__gt/__lt) don't behave any differently chaining as there is no overlap between the queries.

# this is more readable / concise. 
# I'd combine as many of your queries as you can just for readability.
posts = Posts.objects.filter(
        region__in=region,
        cost__gte=min_cost,
        # etc
    )

Now, if you want optional arguments, my suggestion is to use a dictionary of keyword arguments so that you can dynamically populate the kwargs.

keyword_arguments = {
    'region__in': region,
    'cost__gte': min_cost,
    'cost__lt': max_cost,
    'availability__gt': date_min,
    'availability__lt': date_max,
}

if request.GET.get('rooms'):
    keyword_arguments['rooms'] = request.GET['rooms']

if request.GET.get('workers'):
    keyword_arguments['workers'] = request.GET['workers']

posts = Posts.objects.filter(**keyword_arguments)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜