Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Authentication Users and Authorization Custom Permission

Jovan Dandridge
Jovan Dandridge
12,835 Points

What am i missing?

trying and trying but cant seem to figure out

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", "give discounts"),
        )
products/views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import generic
from django.contrib.auth import has_perm

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

    def form_valid(self, form):
        user = self.request.user
        if not user.has_perm('products.can_give_discount'):
            self.object.discount = 0
        self.object.save()
        return super(form_valid, self).form_valid(form)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There are two errors in your code:

:point_right: Before the if statement, define the self.object:

        self.object = form.save(commit=False)  # <-- create self.object to modify

:point_right: Call the parent class from super() not the method:

        return super(Create, self).form_valid(form)  # <-- refer to the parent class

Otherwise, your code will pass. Post back if you need more help. Good Luck!!

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

Chris Freeman , I'm really late to this party but can you explain why it's "products..." in the call to has_perm()? Intuitively it seems it would be models.Product.can_give_discount or something. What explains "products"?

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

Just occurred to me that permissions may be a kind of app-level thing and that's why you have to invoke it that way. Can you confirm if that's the case/let me know what I'm missing?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The has_perm() is part of the Django built-in permissions system.

"When django.contrib.auth is listed in your INSTALLED_APPS setting, it will ensure that three default permissions – add, change and delete – are created for each Django model defined in one of your installed applications"