将url参数传递给ListView queryset [英] Pass url argument to ListView queryset
本文介绍了将url参数传递给ListView queryset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
models.py
class Lab(Model):
acronym = CharField(max_length=10)
class Message(Model):
lab = ForeignKey(Lab)
urls.py
urlpatterns = patterns('',
url(r'^(?P<lab>\w+)/$', ListView.as_view(
queryset=Message.objects.filter(lab__acronym='')
)),
)
我想通过 lab
ListView查询集的关键字参数。这意味着如果 lab
等于 TEST
,则结果查询集将为 Message.objects.filter (lab__acronym ='TEST')
。
I want to pass the lab
keyword argument to the ListView queryset. That means if lab
equals to TEST
, the resulting queryset will be Message.objects.filter(lab__acronym='TEST')
.
我该怎么做?
推荐答案
您需要为此编写自己的视图,然后覆盖 get_queryset
方法:
You need to write your own view for that and then just override the get_queryset
method:
class CustomListView(ListView):
def get_queryset(self):
return Message.objects.filter(lab__acronym=self.kwargs['lab'])
并在URL中使用 CustomListView
也。
这篇关于将url参数传递给ListView queryset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文