0

I hope someone can assist me, I am trying to give custom permissions to a page/view to only 1 user with specific access/permissions. I have a model that looks like this:

class Book(models.Model):
    id = models.UUIDField(
        primary_key = True,
        default=uuid.uuid4,
        editable=False)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    cover = models.ImageField(upload_to='covers/', blank=True)

    class Meta:
        permissions = [
            ('special_status', 'Can read all books'),
    ]
    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('books_detail', args=[str(self.id)])

And a view:

class BooksDetailView(
    LoginRequiredMixin,
    PermissionRequiredMixin,
    DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'books/books_detail.html'
    login_url = 'account_login'
    permission_required = 'books.special_status'

I have given the specific user permissions via the admin console. However, any user other than admin is getting 403 Forbidden. Can anyone tell me how to fix this or what seems to be the problem?

Davit Tovmasyan
  • 3,238
  • 2
  • 20
  • 36
  • 1
    You can debug it like this: `user.has_perm('books.special_status')` – Davit Tovmasyan Dec 06 '20 at 20:54
  • Thanks Davit, all i get is False, though perhaps i am not debugging this correctly this is what i get: `>>> from django.contrib.auth import get_user_model >>> CustomUser = get_user_model() >>> user = CustomUser.objects.get(username="special") >>> user.has_perm('books.special_status') False` – user2782162 Dec 08 '20 at 18:33
  • Do you add permission 'books|book|Can read all books' to specific user in Django site administration? Just found this post when searching similar issue, but my problem is code typo. – T. Elvis Aug 03 '21 at 03:40

0 Answers0