سؤال

I have a couple of different profiles. I want to associate permissions with these profiles. I've done so like this:

class StudentProfile(UserProfile):
    school = models.CharField(max_length=30)
    class Meta:
        permissions = (
            ("is_student","Can access student pages"),
        )

however, when I try and check if that permission exists using has_perm on that profile object, I get an error "'StudentProfile' object has no attribute 'has_perm'" am I not supposed to check for permissions in this way? I've read the docs and that's what I thought I was supposed to do

Edit: After reading the docs again, it seems that has_perm is a method belonging to Users and NOT their profiles. However, when I try to show the permissions:

print user.get_all_permissions()

I get an empty set. Shouldn't I see something like "appname.is_student"

هل كانت مفيدة؟

المحلول

.has_perm is a method on the User object, not on a UserProfile object. If you are trying to validate that a user has the permission has_student, you'd need to do something like this:

user.has_perm('profiles.is_student')

assuming that your StudentProfile model is in a profiles application.

EDIT: To address your rephrased question, you should assign permissions the normal way, either to the group or to a particular user, and use User.has_perm. Your latter example goes completely against the point of the Django permission system.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top