Reputation: 1077
I'm building a website that will include articles. Article content will be stored in a database model. Example:
class article(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_posted = models.DateTimeField(default=timezone.now)
content = models.TextField()
...
I'm wanting to use the built-in admin app for adding new articles to the website. And I'm going to have a group called 'author' which will allow other people to log in to the admin section and add new content.
My issue is with the permissions. I want to give the 'author' group the permission to add, change, view and delete Articles. But I want to restrict this to only articles that they have created. Basically I don't want them to be able to update other author's posts.
Is there a way that I can give them custom permissions so that they only have the ability to change, view, and delete only their own posts?
Upvotes: 0
Views: 402
Reputation: 960
You can use get_queryset for that.
class ArticleModelAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(ArticleModelAdmin, self).get_queryset(request)
return qs.filter(author=request.user)
Don't forget to register your ModelAdmin.
admin.site.register(Article, ArticleModelAdmin)
Upvotes: 1