2. Activate Django Admin Site Add 'django.contrib.admin' to your INSTALLED_APPS setting. Add - django.contrib.auth, django.contrib.contenttypes and django.contrib.messages. Add django.contrib.messages.context_processors.messagesto TEMPLATE_CONTEXT_PROCESSORS and django.contrib.messages.middleware.MessageMiddlewaretoMIDDLEWARE_CLASSES. Determine which of your application’s models should be editable in the admin interface. For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model. Hook the AdminSite instance into your URLconf.
4. ModelAdmin The ModelAdmin class is the representation of a model in the admin interface. These are stored in a file named admin.py in your application. Eg : from django.contrib import admin from myproject.myapp.models import Author class AuthorAdmin(admin.ModelAdmin): // Attributes admin.site.register(Author, AuthorAdmin)
5. Behavior ModelAdmin.list_display fields are displayed on the change list page of the admin ModelAdmin.search_fields Set search_fields to enable a search box on the admin change list page ModelAdmin.ordering Set ordering to specify how lists of objects should be ordered in the Django admin views ModelAdmin.list_display_links Set list_display_links to control which fields in list_display should be linked to the "change" page for an object ModelAdmin.fields if you want to only show a subset of the available fields in the form ModelAdmin.exclude list of field names to exclude from the form
6.
7. Behavior Eg class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('url', 'title', 'content', 'sites') }), ('Advanced options', { 'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name') }), ) To display multiple fields on the same line: { 'fields': (('first_name', 'last_name'), 'address', 'city', 'state'), }
8. admin actions need to make the same change to many objects Eg Suppose you want to make all the cache’s status as True def make_published(modeladmin, request, queryset): queryset.update(status=True) make_published.short_description = "Mark selected stories as published“ The current ModelAdmin An HttpRequest representing the current request, A QuerySet containing the set of objects selected by the user.
9. admin actions def make_available(modeladmin, request, queryset): queryset.update(status=True) make_available.short_description = "Mark selected cache’s as available“ # Just add the actions in the admin model class ArticleAdmin(admin.ModelAdmin): list_display = ['title', 'status'] ordering = ['title'] actions = [make_available] Disable The Actions: admin.site.disable_action('delete_selected‘)
10. options A list of actions to make available on the change list page. ModelAdmin.actions_on_top ModelAdmin.actions_on_bottom Controls where on the page the actions bar appears. By default, the admin changelist displays actions at the top of the page (actions_on_top = True; actions_on_bottom = False). ModelAdmin.actions_selection_counter Controls whether a selection counter is display next to the action dropdown. By default, the admin changelist will display it (actions_selection_counter = True).
11. Hooking AdminSite into URLconf # urls.py from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)),)
12. Hooking AdminSite into URLconf # urls.py from django.conf.urls.defaults import * from myproject.admin import admin_site urlpatterns = patterns('', (r'^myadmin/', include(admin_site.urls)), )
13. Multiple admin sites in the same URLconf # urls.py from django.conf.urls.defaults import * from myproject.admin import basic_site, advanced_site urlpatterns = patterns('', (r'^basic-admin/', include(basic_site.urls)), (r'^advanced-admin/', include(advanced_site.urls)), )
14. Assignments Study the following components ModelAdmin.filter_horizontal ModelAdmin.filter_vertical ModelAdmin.form ModelAdmin.formfield_overrides list_filter list_select_related prepopulated_fields ModelAdmin.radio_fields ModelAdmin.save_as ModelAdmin.save_on_top class InlineModelAdmin class TabularInline class StackedInline ModelAdmin.raw_id_fields