Changes between Version 46 and Version 47 of NewformsAdminBranch


Ignore:
Timestamp:
05/16/08 14:25:24 (7 years ago)
Author:
brosner
Comment:

Fixed up some style and wording. Improved a couple of examples.

Legend:

Unmodified
Added
Removed
Modified
  • NewformsAdminBranch

    v46 v47  
    102102    author = models.ForeignKey(Author)
    103103
    104 class BookOptions(admin.ModelAdmin):
     104class BookAdmin(admin.ModelAdmin):
    105105    list_display = ('title', 'author')
    106106    ordering = ('title',)
     
    108108# Make sure the following are executed exactly once (i.e., watch your imports), or you'll see AlreadyRegistered exceptions.
    109109admin.site.register(Author)
    110 admin.site.register(Book, BookOptions)
    111 }}}
    112 
    113 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 {{{BookOptions}}} class and pass that class as a second argument to {{{admin.site.register()}}}.
     110admin.site.register(Book, BookAdmin)
     111}}}
     112
     113In 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()}}}.
    114114
    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]]
     
    117117'''A proposal 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.
    118118
    119 You'll notice the {{{BookOptions}}} 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:
    120 
    121 {{{
    122 #!python
    123 
    124 class BookOptions(admin.ModelAdmin):
     119You'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:
     120
     121{{{
     122#!python
     123
     124class BookAdmin(admin.ModelAdmin):
    125125    list_display = ('title', 'author')
    126126    ordering = ('title',)
     
    132132        if obj and request.user.username == 'john':
    133133            return obj.author.last_name == 'Dahl'
    134         return super(BookOptions, self).has_change_permission(request, obj)
     134        return super(BookAdmin, self).has_change_permission(request, obj)
    135135}}}
    136136
     
    149149=== Changed Admin.manager option to more flexible hook ===
    150150
    151 As of [4342], the {{{manager}}} option to {{{class Admin}}} no longer exists. This option was undocumented, but we're mentioning the change here in case you used it. In favor of this option, {{{class Admin}}} may now define one of these methods:
    152 
    153   * {{{queryset()}}}
    154   * {{{queryset_add()}}}
    155   * {{{queryset_change()}}}
    156 
    157 These give you much more flexibility.
    158 
    159 (We initially called the new method {{{change_list_queryset}}}, but this was changed in [4584] to be more flexible.)
     151As of [4342], the {{{manager}}} option to {{{class Admin}}} no longer exists. This option was undocumented, but we're mentioning the change here in case you used it. In favor of this option, {{{class Admin}}} may now define a ```queryset``` method:
     152
     153{{{
     154#!python
     155
     156class BookAdmin(admin.ModelAdmin):
     157    def queryset(self, request):
     158        """
     159        Filter based on the current user.
     160        """
     161        return self.model._default_manager.filter(user=request.user)
     162}}}
    160163
    161164=== Changed prepopulate_from to be defined in the Admin class, not database field classes ===
     
    183186from django.contrib import admin
    184187
    185 class MyModelOptions(admin.ModelAdmin):
     188class MyModelAdmin(admin.ModelAdmin):
    186189    prepopulated_fields = {'slug': ('first_name', 'last_name')}
    187190
    188 admin.site.register(MyModel, MyModelOptions)
     191admin.site.register(MyModel, MyModelAdmin)
    189192}}}
    190193
     
    223226   class Admin:
    224227     fields = (
    225        ('group1', {'fields': ('field1','field2'), 'classes': 'collapse'}),
    226        ('group2', {'fields': ('field3','field4'), 'classes': 'collapse wide'}),
    227        )
    228 
    229 # NEW:
    230 class MyModelA_Options(admin.ModelAdmin):
    231     fields = ('field1','field2','field3','field4')  # Renaming is optional
    232 
    233 class MyModelB_Options(admin.ModelAdmin):
     228         ('group1', {'fields': ('field1','field2'), 'classes': 'collapse'}),
     229         ('group2', {'fields': ('field3','field4'), 'classes': 'collapse wide'}),
     230     )
     231
     232# NEW:
     233class MyModelAdmin(admin.ModelAdmin):
     234    fields = ('field1', 'field2', 'field3', 'field4')  # Renaming is optional
     235
     236class AnotherModelAdmin(admin.ModelAdmin):
    234237     fieldsets = (
    235        ('group1', {'fields': ('field1','field2'), 'classes': ('collapse',)}),
    236        ('group2', {'fields': ('field3','field4'), 'classes': ('collapse', 'wide')}),
    237        )
     238         ('group1', {'fields': ('field1','field2'), 'classes': ('collapse',)}),
     239         ('group2', {'fields': ('field3','field4'), 'classes': ('collapse', 'wide')}),
     240     )
    238241}}}
    239242
     
    246249#!python
    247250from django.contrib import admin
    248 class Child_Inline(admin.TabularInline):
     251
     252class ChildInline(admin.TabularInline):
    249253    model = Child
    250254    extra = 3
    251255
    252 class Parent_admin(admin.ModelAdmin):
     256class ParentAdmin(admin.ModelAdmin):
    253257    model = Parent
    254     inlines = [Child_Inline]
     258    inlines = [ChildInline]
    255259}}}
    256260
     
    262266{{{
    263267#!python
     268
    264269# OLD:
    265270class MyModel(models.Model):
    266271    field1 = models.ForeignKey(AnotherModel, raw_id_admin=True)
     272   
    267273    class Admin:
    268274        pass
    269275
    270276# NEW:
    271 class MyModel_Options(admin.ModelAdmin):
     277class MyModelAdmin(admin.ModelAdmin):
    272278    model = MyModel
    273279    raw_id_fields = ('field1',)
     
    279285opposed to using oldforms. If you are relying on the oldforms, you will need
    280286to modify your code/templates to work with newforms.
     287
Back to Top