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