Changes between Version 59 and Version 60 of NewformsAdminBranch
- Timestamp:
- Jul 9, 2008, 12:47:33 PM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
NewformsAdminBranch
v59 v60 62 62 #!python 63 63 64 # models.py 65 66 from django.contrib import admin 64 # project-level admin.py 65 66 from django.contrib import admin 67 from myproject.myapp.models import Book, Author 68 from myproject.anotherapp.models import Musician, Instrument 67 69 68 70 site1 = admin.AdminSite() … … 77 79 78 80 from django.conf.urls.defaults import * 79 from myproject. myapp.modelsimport site1, site281 from myproject.admin import site1, site2 80 82 81 83 urlpatterns = patterns('', … … 106 108 author = models.ForeignKey(Author) 107 109 110 # a sample admin.py file (in same app) 111 from django.contrib import admin 112 from myproject.myapp.models import Author, Book 113 108 114 class BookAdmin(admin.ModelAdmin): 109 115 list_display = ('title', 'author') 110 116 ordering = ('title',) 111 117 112 # Make sure the following are executed exactly once (i.e., watch your imports), or you'll see AlreadyRegistered exceptions.113 118 admin.site.register(Author) 114 119 admin.site.register(Book, BookAdmin) … … 116 121 117 122 In 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 #!python124 # a sample models.py file125 from django.db import models126 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 file139 from django.contrib import admin140 141 class BookAdmin(admin.ModelAdmin):142 list_display = ('title', 'author')143 ordering = ('title',)144 145 # a sample __init__.py file146 from django.contrib import admin147 from project.app.models import Author, Book148 from project.app.admins import BookAdmin149 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 }}}155 123 156 124 You'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: