Django 过滤问题

发布于 2024-08-10 11:55:34 字数 1479 浏览 4 评论 0原文

我正在尝试在我的一个视图中设置过滤器查询...基本上我的代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪淡了忧伤 2024-08-17 11:55:34

在我看来你好像缺少一个表达。

(r'^inventory/year/(?P<year>d{4})/?

匹配一系列 4 位数字, 匹配什么?

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

这与 make 变量相匹配。很高兴知道我是否错了!

, 'app.vehicles.views.year_filter'), (r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),

匹配一系列 4 位数字, 匹配什么?

这与 make 变量相匹配。很高兴知道我是否错了!

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

(r'^inventory/year/(?P<year>d{4})/?

<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!

, '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?

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

旧人哭 2024-08-17 11:55:34

看起来在你的 URLConf 中,在你已经提到的那些之上,你可能有一个覆盖下面的映射 -
就像这样说 -

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

这导致库存中的所有调用都命中方法库存。

这是因为 Django 以串行方式经历它。任何首先匹配它的 URL 都会命中它。
为了克服这个问题,您可以通过以下方式重新排序 url -

(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'),

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'),
╭ゆ眷念 2024-08-17 11:55:34

《古兰经》走在正确的道路上。尝试在 URLconf 中添加以下内容:

(r'^inventory/

正则表达式 r'^inventory/$' 严格匹配,除了“/”之外没有任何内容。这应该会导致其他 URL 映射到正确的视图。

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

正则表达式 r'^inventory/$' 严格匹配,除了“/”之外没有任何内容。这应该会导致其他 URL 映射到正确的视图。

, 'app.vehicles.views.year_filter'), (r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),

正则表达式 r'^inventory/$' 严格匹配,除了“/”之外没有任何内容。这应该会导致其他 URL 映射到正确的视图。

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

(r'^inventory/

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

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

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

, '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.

秋风の叶未落 2024-08-17 11:55:34

d{4} 始终与四个 d 完全匹配,dddd。这就是你想要的吗?

(r'^inventory/year/(?P<year>d{4})/?

我怀疑你需要用 \ 转义 d ,比较:

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

我怀疑你需要用 \ 转义 d ,比较:


, 'app.vehicles.views.year_filter'),
, 'app.vehicles.views.year_filter'),

我怀疑你需要用 \ 转义 d ,比较:

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

(r'^inventory/year/(?P<year>d{4})/?

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

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

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


, 'app.vehicles.views.year_filter'),
, 'app.vehicles.views.year_filter'),

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文