Django 过滤问题
我正在尝试在我的一个视图中设置过滤器查询...基本上我的代码如下所示:
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,})
到目前为止,当我尝试最后两个视图中的任何一个时,我只从第一个视图中获取查询集,即存货。 URLConf 文件如下所示:
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
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'),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来你好像缺少一个表达。
匹配一系列 4 位数字,
匹配什么?这与 make 变量相匹配。很高兴知道我是否错了!
It looks to me like you're missing an expression.
<year>
matches a series of 4 digits, what does<make>
match?That matches something to the make variable. Happy to know if I'm wrong!
看起来在你的 URLConf 中,在你已经提到的那些之上,你可能有一个覆盖下面的映射 -
就像这样说 -
这导致库存中的所有调用都命中方法库存。
这是因为 Django 以串行方式经历它。任何首先匹配它的 URL 都会命中它。
为了克服这个问题,您可以通过以下方式重新排序 url -
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 -
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 -
《古兰经》走在正确的道路上。尝试在 URLconf 中添加以下内容:
正则表达式 r'^inventory/$' 严格匹配,除了“/”之外没有任何内容。这应该会导致其他 URL 映射到正确的视图。
Koran is on the right path. Try having this in your URLconf:
The regex r'^inventory/$' matches strictly just that, nothing past the '/'. This should cause the other URLs to map to the proper views.
d{4} 始终与四个 d 完全匹配,
, 'app.vehicles.views.year_filter'),dddd
。这就是你想要的吗?我怀疑你需要用
\
转义d
,比较:d{4} always matches exactly four d's,
, 'app.vehicles.views.year_filter'),dddd
. Is that what you want?I suspect you need to escape
d
with\
, compare: