I am using a from wizard not only to create but also to edit a model (as described here: Django Form Wizard to Edit Model). This works fine, but now I need to check, if the request user is allowed to edit. In my model I have a field, so only the owner should be allowed to edit:

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

Do you have any idea how to do this? Thanks!

有帮助吗?

解决方案

You may want to add the following to the get_form_initial method of your wizard:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top