gpt4 book ai didi

Python Django : Use Form Wizard to Edit Model and check if request user is allowed to edit

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:49 26 4
gpt4 key购买 nike

我使用 from 向导不仅可以创建模型,还可以编辑模型(如下所述: Django Form Wizard to Edit Model )。这工作正常,但现在我需要检查是否允许请求用户进行编辑。在我的模型中,我有一个字段,因此只有所有者才有权编辑:

class Document(models.Model):
...
owner = models.ForeignKey(User, editable=False)
...

你知道如何做到这一点吗?谢谢!

最佳答案

您可能需要将以下内容添加到向导的 get_form_initial 方法中:

from django.core import exceptions

class DocumentWizard(SessionWizardView):
# ...

def get_form_initial(self, step):
# ... determine document_id
document = Document.objects.get(id=document_id)
if self.request.user == document.owner:
document_dict = model_to_dict(document)
return document_dict
else:
raise exceptions.PermissionDenied

关于Python Django : Use Form Wizard to Edit Model and check if request user is allowed to edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com