如何根据当前 URL 中的 ID 过滤 ListView 输出
How to filter a ListView output based on ID from current URL
我的项目是购物清单。我有两个模型:ShoppingList 和 ShoppingItem,如下所示:
models.py
class ShoppingItem (Model):
name = models.CharField(max_length=50, null=False)
count = models.IntegerField(null=False)
list = models.ForeignKey(ShoppingList, on_delete=models.CASCADE, related_name='shopping_items')
date_created = models.DateTimeField(auto_now_add=True)
urls.py
urlpatterns = [
path('ListDetails/<int:pk>', views.ListDetailUpdateView.as_view(), name='listdetailupdate'),
]
views.py
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
queryset = ShoppingItem.objects.filter(list = XXXX)
我需要 return 部分 url 但在 ListView(where"XXXX")
中的函数
我的想法是删除 url 的最后一部分(例如,当我输入列表编号 2 时,我有地址 http://127.0.0.1:8000/ListDetails/2
)并用这样的函数替换 "XXXX"。
在我看来应该是这样的:
queryset = ShoppingItem.objects.filter(list = int(request.path.split('/')[-1])
如果有更好的方法,我会提出所有建议
类似的东西
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
def get_queryset(self):
return ShoppingItem.objects.filter(list=self.request.resolver_match.kwargs['pk'])
重写 get_queryset
方法是一种可行的方法。
调用super()
获取父方法返回的queryset。对其进行过滤应该可以正常工作。
与 url 模式匹配的 pk
将在 self.kwargs
中可用。
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
def get_queryset(self):
qs = super(ListDetailUpdateView, self).get_queryset()
return qs.filter(list=self.kwargs.get('pk'))
我还会考虑将 list
字段名称更改为 shopping_list
或类似名称,因为它会遮盖 built-in list()
函数。
我的项目是购物清单。我有两个模型:ShoppingList 和 ShoppingItem,如下所示:
models.py
class ShoppingItem (Model):
name = models.CharField(max_length=50, null=False)
count = models.IntegerField(null=False)
list = models.ForeignKey(ShoppingList, on_delete=models.CASCADE, related_name='shopping_items')
date_created = models.DateTimeField(auto_now_add=True)
urls.py
urlpatterns = [
path('ListDetails/<int:pk>', views.ListDetailUpdateView.as_view(), name='listdetailupdate'),
]
views.py
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
queryset = ShoppingItem.objects.filter(list = XXXX)
我需要 return 部分 url 但在 ListView(where"XXXX")
中的函数我的想法是删除 url 的最后一部分(例如,当我输入列表编号 2 时,我有地址 http://127.0.0.1:8000/ListDetails/2
)并用这样的函数替换 "XXXX"。
在我看来应该是这样的:
queryset = ShoppingItem.objects.filter(list = int(request.path.split('/')[-1])
如果有更好的方法,我会提出所有建议
类似的东西
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
def get_queryset(self):
return ShoppingItem.objects.filter(list=self.request.resolver_match.kwargs['pk'])
重写 get_queryset
方法是一种可行的方法。
调用super()
获取父方法返回的queryset。对其进行过滤应该可以正常工作。
与 url 模式匹配的 pk
将在 self.kwargs
中可用。
class ListDetailUpdateView(ListView):
model = ShoppingItem
template_name = 'xlist_app/ListDetailUpdateView.html'
context_object_name = 'products'
def get_queryset(self):
qs = super(ListDetailUpdateView, self).get_queryset()
return qs.filter(list=self.kwargs.get('pk'))
我还会考虑将 list
字段名称更改为 shopping_list
或类似名称,因为它会遮盖 built-in list()
函数。