Skip to content

Instantly share code, notes, and snippets.

@ab623
ab623 / is_owner_permission.py
Last active May 13, 2023 01:36
A view decorator to be used to check the model is owned by the logged in user
from django.core.exceptions import PermissionDenied
from django.apps import apps
def is_owner_permission(model=None, url_field='pk', model_field="owner"):
"""
A decorator to be used on a function based view that will check if
a model instance belongs to the logged in user
Keyword arguments:
model=None - Name of the model to check. Can be a model