Changes between Version 59 and Version 60 of NewformsAdminBranch


Ignore:
Timestamp:
Jul 9, 2008, 12:47:33 PM (16 years ago)
Author:
Brian Rosner
Comment:

edited more of the page to reflect current status removing bits no longer relevant in the common case

Legend:

Unmodified
Added
Removed
Modified
  • NewformsAdminBranch

    v59 v60  
    6262#!python
    6363
    64 # models.py
    65 
    66 from django.contrib import admin
     64# project-level admin.py
     65
     66from django.contrib import admin
     67from myproject.myapp.models import Book, Author
     68from myproject.anotherapp.models import Musician, Instrument
    6769
    6870site1 = admin.AdminSite()
     
    7779
    7880from django.conf.urls.defaults import *
    79 from myproject.myapp.models import site1, site2
     81from myproject.admin import site1, site2
    8082
    8183urlpatterns = patterns('',
     
    106108    author = models.ForeignKey(Author)
    107109
     110# a sample admin.py file (in same app)
     111from django.contrib import admin
     112from myproject.myapp.models import Author, Book
     113
    108114class BookAdmin(admin.ModelAdmin):
    109115    list_display = ('title', 'author')
    110116    ordering = ('title',)
    111117
    112 # Make sure the following are executed exactly once (i.e., watch your imports), or you'll see AlreadyRegistered exceptions.
    113118admin.site.register(Author)
    114119admin.site.register(Book, BookAdmin)
     
    116121
    117122In this example, we register both {{{Author}}} and {{{Book}}} with the {{{AdminSite}}} instance {{{django.contrib.admin.site}}}. {{{Author}}} doesn't need any custom admin options, so we just call {{{admin.site.register(Author)}}}. {{{Book}}}, on the other hand, has some custom admin options, so we define a {{{BookAdmin}}} class and pass that class as a second argument to {{{admin.site.register()}}}.
    118 
    119 In this example, the admin options still live in the {{{models.py}}} file. But there's nothing that requires them to do so. The only requirement is that the {{{register()}}} calls are executed at some point, and putting them in the {{{models.py}}} is an easy way to ensure that. We'll likely come up with a nice convention for this.[[BR]]
    120 
    121 '''A proposed convention''': Specifying all admin options in a file called {{{admins.py}}}, and import it in the {{{__init__.py}}} file of your application module to do the registering during the initialization. In this case the above example would look like this:
    122 {{{
    123 #!python
    124 # a sample models.py file
    125 from django.db import models
    126 
    127 class Author(models.Model):
    128     first_name = models.CharField(max_length=30)
    129     last_name = models.CharField(max_length=30)
    130 
    131     def __unicode__(self):
    132         return u'%s %s' % (self.first_name, self.last_name)
    133 
    134 class Book(models.Model):
    135     title = models.CharField(max_length=100)
    136     author = models.ForeignKey(Author)
    137 
    138 # a sample admins.py file
    139 from django.contrib import admin
    140 
    141 class BookAdmin(admin.ModelAdmin):
    142     list_display = ('title', 'author')
    143     ordering = ('title',)
    144 
    145 # a sample __init__.py file
    146 from django.contrib import admin
    147 from project.app.models import Author, Book
    148 from project.app.admins import BookAdmin
    149 
    150 # Make sure the following are executed exactly once (i.e., watch your imports), or you'll see AlreadyRegistered exceptions.
    151 # Meaning if you use project.app, be sure to use project.app everywhere, including in url configuration (i.e. use 'project.app.views')
    152 admin.site.register(Author)
    153 admin.site.register(Book, BookAdmin)
    154 }}}
    155123
    156124You'll notice the {{{BookAdmin}}} class looks a lot like the old-style {{{class Admin}}}. Almost all of the old {{{class Admin}}} options work exactly the same, with one or two exceptions. (For the options that have changed, we've made them '''much''' more powerful.) In addition to the classic options such as {{{list_display}}} and {{{ordering}}}, the {{{ModelAdmin}}} class introduces a wealth of extra hooks you can use to customize the admin site for that particular model. For example:
Back to Top