views:

80

answers:

1

I've been able to implement a filtering solution for my app...so far I have filtering by year, make and body style for a vehicle inventory. What I want to do is to filter the record this way:

Lets assume I choose Acura in make, in the resultant page, if I choose one of the other filters i.e. year or body style, I'll need only the records containing Acura and the chosen year or body style.

My code for one of the filters is as below:

def year_filter(request, year):   
   vehicle_query = Vehicle.objects.filter(
 common_vehicle__year__year__exact=year
 ).exclude(status__status='Incoming')

   vehicle_list = vehicle_query.order_by(
 'common_vehicle__series__model__manufacturer__manufacturer', 
 'common_vehicle__series__model__model', 
 'common_vehicle__year'
)

   vehicle = paginate(request, vehicle_list)

   year_count = vehicle_query.order_by(
 '-common_vehicle__year__year')
            .values('common_vehicle__year__year')
            .annotate(count=Count('id')
   )
   make_count = vehicle_query.order_by(
      'common_vehicle__series__model__manufacturer__manufacturer')
      .values('common_vehicle__series__model__manufacturer__manufacturer')
      .annotate(count=Count('id')
)
    style_count = vehicle_query.order_by(
   'common_vehicle__body_style__style')
       .values('common_vehicle__body_style__style')
       .annotate(count=Count('id')
)
    color_count = vehicle_query.order_by(
       'exterior_colour__exterior_colour')
       .values('exterior_colour__exterior_colour')
       .annotate(count=Count('id')
)

    return render_to_response('vehicles.html', {
      'vehicle': vehicle, 
      'make_count': make_count, 
      'year_count': year_count, 
      'style_count': style_count,
    })
+2  A: 

Firstly, some style advice: you're making four separate queries, but each of them start in exactly the same way:

Vehicle.objects.filter(
   common_vehicle__series__model__manufacturer__manufacturer=make
).exclude(
   status__status='Incoming'
)

For the sake of readability, please consider assigning this to a variable, eg vehicle_query, which you can then use in each subsequent query:

vehicle_list = vehicle_query.order_by(
   'common_vehicle__series__model__manufacturer__manufacturer', 
   'common_vehicle__series__model__model', 
   'common_vehicle__year'
)

The query isn't evaluated until the last minute, so this won't affect efficiency, but will be much more readable.

Now, to answer your question: you'll obviously need some way of saving the chosen values from one view to the next. I can't quite understand how the other views fit with this one - my initial inclination would be to put it in a hidden field, or in the querystring. But maybe saving it in the session would be the best idea:

if request.POST:
    chosen_vehicle = request.POST['vehicle_type']
    request.session['vehicle_type'] = chosen_vehicle.id
    return HttpResponseRedirect('/next/view/')

and in the next view:

previously_chosen = request.session['vehicle_type']
filtered = Vehicle.objects.get(id=previously_chosen)

Does that work?

Daniel Roseman
let me try this out and see if it works...just woke up
this works out fine...thnx
I'd written the code but never got to test it out...I just assumed that it works fine. Apparently it doesn't...the sessions aren't saved. I have five views like the one above...for year, make, series, color, and body style. I really need help on this coz I'm out of ideas
Can't really help without seeing how you're using sessions - maybe post a new question? But see the sessions documentation first (http://docs.djangoproject.com/en/dev/topics/http/sessions/) to see if there's anything obvious you're missing.
Daniel Roseman
I'm really in the dark as to how I can go about using the sessions...I've looked at the documentation but I can't get it to work. I also need to figure out how to make all the filters into one as currently I have five individual filters (I think this is why I'm having difficulty with it). I really need this to work...it's the last part in my app
I'd love to help, but can't without some idea of what you're currently doing and what's going wrong. Your other (closed) question was just a duplicate of this one, but you need to ask about the actual problem you're having with sessions, including some code that shows the issue.
Daniel Roseman
I've posted the entire code for filters...I haven't figured out how to use the sessions, so I guess the answer will start from here. Ideally I want to have only one filter view instead of the current five, but if the sessions can work well in the current five views it's still OK. I hope you can help me out now.