Changes between Version 55 and Version 56 of NewformsAdminBranch


Ignore:
Timestamp:
Jul 8, 2008, 5:12:28 PM (16 years ago)
Author:
jashugan@…
Comment:

presented how to go about separating out admin functionality into its own file

Legend:

Unmodified
Added
Removed
Modified
  • NewformsAdminBranch

    v55 v56  
    115115In 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]]
    116116
    117 '''A proposed convention''': Specifying all admin options in a file called {{{admin.py}}}, and import it in the {{{__init__.py}}} file of your application module to do the registering during the initialization.
     117'''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:
     118{{{
     119#!python
     120# a sample models.py file
     121from django.db import models
     122
     123class Author(models.Model):
     124    first_name = models.CharField(max_length=30)
     125    last_name = models.CharField(max_length=30)
     126
     127    def __unicode__(self):
     128        return u'%s %s' % (self.first_name, self.last_name)
     129
     130class Book(models.Model):
     131    title = models.CharField(max_length=100)
     132    author = models.ForeignKey(Author)
     133
     134# a sample admins.py file
     135from django.contrib import admin
     136
     137class BookAdmin(admin.ModelAdmin):
     138    list_display = ('title', 'author')
     139    ordering = ('title',)
     140
     141# a sample __init__.py file
     142from django.contrib import admin
     143from models import Author, Book
     144from admins import BookAdmin
     145
     146admin.site.register(Author)
     147admin.site.register(Book, BookAdmin)
     148}}}
    118149
    119150You'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