from django.contrib import admin from calendarium.models import Event class EventAdmin(admin.ModelAdmin): list_display = ('name', 'start', 'end', 'source') list_filter = ('source',) search_fields = ['name', 'description'] date_hierachy = 'start' actions = None delete_confirmation_template = 'calendarium/delete_confirmation.html' def save_model(self, request, obj, form, change): """Sets the source to "admin". """ if not obj.id: obj.source = Event.SOURCE_ADMIN obj.save() def get_readonly_fields(self, request, obj=None): """Sets all fields read only if source is "import". """ if obj and obj.source == obj.SOURCE_IMPORT: return ['name', 'description', 'start', 'end'] return [] admin.site.register(Event, EventAdmin)