在 Django 3 中,我想洗掉一個物件,并在用戶確認洗掉后創建一個不相關的物件。我在下面撰寫的代碼會在用戶單擊“洗掉”后立即創建不相關的物件,但在用戶確認洗掉之前。我希望在確認洗掉后創建不相關的物件。
class PromoteQuestionToMeta(DeleteView):
model = QuestionList
fields = []
template_name = 'search/questionlist_confirm_delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
questionlist_object = self.object
MetaQuestionList.objects.create(question=questionlist_object.question, created_by=questionlist_object.created_by)
return context
def get_success_url(self):
return reverse_lazy('home:home')
uj5u.com熱心網友回復:
您可以覆寫.delete(…)方法 [Django-doc]并創建一個物件:
class PromoteQuestionToMeta(DeleteView):
model = QuestionList
fields = []
template_name = 'search/questionlist_confirm_delete.html'
def delete(self, *args, **kwargs):
result = super().delete(*args, **kwargs)
MetaQuestionList.objects.create(
question=self.object.question,
created_by=self.object.created_by
)
return result
def get_success_url(self):
return reverse_lazy('home:home')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403002.html
標籤:
