views:

92

answers:

4

I'm trying to set up a filter query in one of my views...basically my code looks as below:

def inventory(request):   
   vehicle = Vehicle.objects.all().exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer', 'common_vehicle__series__model__model', 'common_vehicle__year')

   year_count = Vehicle.objects.exclude(status__status='Incoming').order_by('-common_vehicle__year__year').values('common_vehicle__year__year').annotate(count=Count('id'))
   make_count = Vehicle.objects.exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer').values('common_vehicle__series__model__manufacturer__manufacturer').annotate(count=Count('id'))

   return render_to_response('vehicles.html', {'vehicle': vehicle, 'make_count': make_count, 'year_count': year_count,})

def year_filter(request, year):
   vehicle = Vehicle.objects.filter(common_vehicle__year__year=year)

   return render_to_response('filter.html', {'vehicle':vehicle,})

def make_filter(request, make):
   vehicle = Vehicle.objects.filter(common_vehicle__series__model__manufacturer__manufacturer=make).exclude(status__status='Incoming')

   return render_to_response('filter.html', {'vehicle':vehicle,})

So far when I try any of the last two views, I'm only getting the query set from the first view i.e. inventory. The URLConf file looks as below:

(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
A: 

It looks like in your URLConf, above the ones you have already mentioned, you might have a mapping which overrides the ones below - say like -

(r'^inventory/', 'app.vehicles.views.inventory'),

which is causing all the calls in inventory to hit the method inventory.

This is because Django goes through it in a serial fashion. Any URL which matches it first will hit it. To overcome this, you reorder the urls in the following way -

(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
(r'^inventory/', 'app.vehicles.views.inventory'),
Koran
I did have a mapping like that prior to the ones for make and year...I've updated the URLConf as you've specified, but it's still hitting the inventory method
The way I would solve the problem is as follows:1. Make a backup of urlconf.2. Remove all lines from urlconf which can hit views.inventory3. Try to get the remaining urls to hit year_filter and make_filter.4. Once done, put the urls back one by one, in proper positions, until everything is proper.
Koran
A: 

Koran is on the right path. Try having this in your URLconf:

(r'^inventory/$', 'app.vehicles.views.inventory'),
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),

The regex r'^inventory/$' matches strictly just that, nothing past the '/'. This should cause the other URLs to map to the proper views.

hora
OK...at least now I think the issue is the last two regex's...I get 404's for them now
+1  A: 

It looks to me like you're missing an expression.

(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),

<year> matches a series of 4 digits, what does <make> match?

(r'^inventory/make/(?P<make>[-\w]+)/', 'app.vehicles.views.make_filter'),

That matches something to the make variable. Happy to know if I'm wrong!

bennylope
I'd forgetten all about the matching bit...thanks bennylope. This however gives me a blank queryset...so I don't know what could be the issue
my bad...just seen my mistake...now the make filter works.
A: 

d{4} always matches exactly four d's, dddd. Is that what you want?

(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),

I suspect you need to escape d with \, compare:

(r'^inventory/year/(?P<year>\d{4})/?$', 'app.vehicles.views.year_filter'),
sanmai
this is what I want...I'd however figured out that I'd left out the escaping part...so I have (r'^inventory/year/(?P<year>\d{4})/$', 'app.vehicles.views.year_filter'), This however gives me: Related Field has invalid lookup: year
I've figured it out...adding __exact to my year_count query fixed the error.