DJango admin interface


Published on

basic overview of django admin interface

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

DJango admin interface

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