Permissions: Let only the user who created the model see the detail view for the model

15 views
Skip to first unread message

pieceof...@gmail.com

unread,
Aug 7, 2017, 6:11:08 PM8/7/17
to Django users
Hey,

I am building an app where a user needs to create Bundle models.  I am providing a Bundle detail view with options for the user to add components or edit the components in their Bundle model.  I currently have it where any signed in user can access these bundles.  I would like to restrict access to this detail page to only the user who created the Bundle model (and the admins as well if possible).  What is the best way to go about this? 

models.py
class Bundle(models.Model):
    user
= models.ForeignKey(User, on_delete=models.CASCADE)

urls.py
from . import views

urlpatterns
=[
    url
(r'^(?P<pk>\d+)/$', views.BundleDetail.as_view(), name='bundle_detail'),
]

views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.decorators import method_decorator
from django.views import generic
from .models import Bundle

@method_decorator(login_required, name='dispatch')
class BundleDetail(LoginRequiredMixin, generic.DetailView):
    model
= Bundle




Reply all
Reply to author
Forward
0 new messages