我可以让更新代码在类视图中延迟运行吗?
我想要的是必须首先执行更新代码。
因为我必须在输出类别列表之前更新用户模型
class MyShortcutListByCategory(ListView):
def get_queryset(self):
user = self.request.user.profile.shortcut_user_id
# print("user : ", user)
print("self.request.user : ", self.request.user)
if user == "me":
user = self.request.user
else:
user = User.objects.get(username=user)
slug = self.kwargs['slug']
if slug == '_none':
category = None
else:
category = Category.objects.get(slug=slug)
pf = Profile.objects.filter(Q(user=self.request.user)).update(selected_category_id = category.id)
print('category id update 성공')
return MyShortCut.objects.filter(category=category, author=user).order_by('created')
def get_context_data(self, *, object_list=None, **kwargs):
context = super(type(self), self).get_context_data(**kwargs)
context['category_list'] = Category.objects.all()
context['posts_without_category'] = MyShortCut.objects.filter(category=None,author=self.request.user).count()
slug = self.kwargs['slug']
if slug == '_none':
context['category'] = '미분류'
else:
category = Category.objects.get(slug=slug)
context['category'] = category
return context
更新
category = Category.objects.get(slug=slug)
pf = Profile.objects.filter(Q(user=self.request.user)).update(selected_category_id = category.id)