Monday, April 9, 2012

Django Blog Zinnia Limit User to Own Entry/Post

I just need a simple yet flexible django based blog application, and found django-blog-zinnia. I really admire how simple it is. The only major adjustment that I need is to limit user to his/her own blog entry/post.

Combining basic django (admin) knowledge with soome info from django-blog-zinnia documentation, I found a simple solution. Of course this is related with django-blog-zinnia's admin interface, and there is a documentation about how to make some modification to the EntryAdmin. Read more from here.

The idea is to create a django application and create admin.py on that app's top level directory (the same location with models.py).

Here is how my admin.py codes look like:
############# admin.py #################

from django.contrib import admin
from zinnia.models import Entry
from zinnia.admin.entry import EntryAdmin

class FilteredEntryAdmin(EntryAdmin):
    def queryset(self, request):
        qs = super(FilteredEntryAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(authors=request.user)

admin.site.unregister(Entry)
admin.site.register(Entry, FilteredEntryAdmin)

############# end of admin.py #################

Remember to add the newly created app in settings.py INSTALLED_APPS, and make sure you put it below zinnia.

Django's documentation is quite complete and comprehensive. To get more information about ModelAdmin, queryset, etc, go to this link. From there I got the idea of overriding the queryset and use filter to limit the entry based on user.


No comments:

Post a Comment