Django:每个用户的UpdateView限制 [英] Django: UpdateView restrict per user
问题描述
我有一个用户可以创建和编辑自己的列表的网站。
我使用通用视图CreateView来允许用户创建列表。 >
我想使用通用视图UpdateView来允许他们编辑列表,但是 login_required = True
是不够的在这种情况下,由于只有列表创建者可以编辑他/她的列表。
2个问题:
1 )是否有任何可以在URLconf中指定的参数来添加这个限制?
2)我可以强加这些通用视图应该只适用于POST而不是GET?
谢谢
您可以覆盖
: UpdateView
中的get_queryset
def get_queryset(self)
/ pre>
base_qs = super(YourListUpdateView,self).get_queryset()
返回base_qs.filter(user = self.request.user)
I have a site where users can create and edit their own lists.
I'm using the generic view CreateView to allow users to create lists.
I would like to use the generic view UpdateView to allow them to edit the lists, but the
login_required=True
is not enough in this case, since only the list creator can edit his/her list.2 questions:
1) is there any parameter that I can specify in the URLconf to add this restrictions?
2) can I impose the those generic views should only work with POST and not GET?
Thanks
解决方案You could override
get_queryset
on theUpdateView
:def get_queryset(self): base_qs = super(YourListUpdateView, self).get_queryset() return base_qs.filter(user=self.request.user)
这篇关于Django:每个用户的UpdateView限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!