Python Django Authentication Users and Authorization Custom Permission

Gerardo Moxca Ruiz
Gerardo Moxca Ruiz
5,490 Points

What's wrong with my code?

Can't figure out what's wrong with the permissions attribute on the Meta class. When checking my code it throws me:

Bummer: Try again!

products/models.py
from django.core.urlresolvers import reverse
from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    price = models.DecimalField()
    discount = models.DecimalField(blank=True, null=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("products:detail", kwargs={"pk": self.pk})

    class Meta:
        permissions = (
            ("can_give_discount", "...")
        )
products/views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import generic

from . import models


class List(generic.ListView):
    model = models.Product


class Detail(generic.DetailView):
    model = models.Product


class Create(LoginRequiredMixin, generic.CreateView):
    fields = ("name", "description", "discount", "price")
    model = models.Product

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 22,201 Points

The class Meta permissions tuple has an issue-- you need an extra comma after the tuple. One of the odd parsing features of python is that it can't tell a single element tuple from an expression until there is another comma following it.

This should fix it.

class Meta:
        permissions = (
            ("can_give_discount", "..."),
        )