Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lesson 9 - The Django Admin

Lesson 9 - The Django Admin

Dana Spiegel

December 05, 2012
Tweet

More Decks by Dana Spiegel

Other Decks in Technology

Transcript

  1. Review Softball Game Tracker 2 Team Game Roster Roster Player

    Statistic Statistic 1..n 1..n n..1 home away n..1 n..1
  2. The Django Admin • Django provides a very easy and

    nice admin interface • Can use it to manage any model you create, plus relations and actions • Admin configuration lives in admin.py • Each model gets its own ModelAdmin (unless inlining...) • Only admins can get access to admin interface • Admin provides filtering, searching, and sorting 3 from django.contrib import admin from models import Team, Player class TeamAdmin(admin.ModelAdmin): search_fields = ('name', 'players__name', ) class PlayerAdmin(admin.ModelAdmin): list_display = ('name', 'number', 'team', ) search_fields = ('name', 'number', 'team__name', ) list_filter = ('team', ) admin.site.register(Team, TeamAdmin) admin.site.register(Player, PlayerAdmin)
  3. ModelAdmin • Each model should have its own ModelAdmin class

    defined • Register each ModelAdmin with the AdminSite • Each ModelAdmin has a number of settings, all defined as attributes: • fields: a list or tuple of fields shown on the Model change form • list_display: a list or tuple of fields/methods shown on the Model list page • string: interpreted as the name of the field in the Model • If the field is ForeignKey, the __unicode__() method is used to display the value • method: interpreted as a method defined in the ModelAdmin 4 admin.site.register(Team, TeamAdmin) class GameAdmin(admin.ModelAdmin): list_display = ('id', 'played_on', 'location', 'home_team', 'away_team', ) def home_team(self, obj): return obj.home_roster.team.name
  4. ModelAdmin (cont’d.) • More ModelAdmin attributes: • fieldsets: allows customized

    grouping of different fields on the Model change form • list_filter: a list or tuple of fields that are used as filters to show only certain objects • search_fields: a list or tuple of fields (extending into related objects) that should be searched to show only objects with matching values 5 class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('url', 'title', 'content', 'sites') }), ('Advanced options', { 'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name') }), ) class PlayerAdmin(admin.ModelAdmin): list_display = ('name', 'number', 'team', ) search_fields = ('name', 'number', 'team__name', ) list_filter = ('team', )
  5. InlineModelAdmin • Allow the inclusion of related objects in the

    Model change form • Show up as a table of additional objects with defined fields • Must define a TabularInline class that works like ModelAdmin • Must include a reference to the Model • In the ModelAdmin where the inlines will be shown, define inlines, a tuple or list of the TabularInline classes 6 class StatisticAdmin(admin.TabularInline): model = Statistic fields = ('player', 'at_bats', 'runs', 'singles', 'doubles', 'triples', 'home_runs', 'rbis', 'walks', ) max_num = 12 extra = 12 class RosterAdmin(admin.ModelAdmin): list_display = ('team', 'home_game', 'away_game', ) inlines = (StatisticAdmin, )
  6. Using the Admin • Admin allows creation, deletion, and editing

    of each type of Model defined • Be careful when using! This is live data! • Use the manage.py createsuperuser to create a user that can access the admin in Django 7
  7. Building an Admin Interface • In class exercise: Build the

    admin for the Softball Tracker • Create ModelAdmin objects for: • Team • Player • Game • Roster • Create TabularInline for Statistic and install it in the RosterAdmin • Show team names on the GameAdmin 8
  8. Homework: Admin • Homework: • Finish building admin interface •

    Add additional columns to different lists 9