Monday, September 1, 2014

Re: How create a custom permission for a model?

> Can't create other users
> Can't create other topics or subtopics
These can be done using built in permissions and groups

Something like this might work for the rest:

class NewsAdmin(models.Model):

   
def get_queryset(self, request):
        queryset
= super(NewsAdmin, self).get_queryset(request):
       
if request.user.groups.filter(name="Author").exist():  # is the user an author?
             queryset
= queryset.filter(author=self.user)  # only allow users to edit their own posts
       
return queryset

   
def get_readonly_fields(self, request, obj=None):
        readonly_fields
= super(NewsAdmin, self).get_readonly_field(request, obj)
       
if request.user.groups.filter(name="Author").exist():  # is the user an author?
            readonly_fields
+= ['published']  # don't allow authors to change the publish status
       
return readonly_fields



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4bde0473-d7c2-4ae2-a0f7-1521c34d80a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment