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

StackOverflow https://stackoverflow.com/questions/21407664

문제

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