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()}}}. |
| 110 | admin.site.register(Book, BookAdmin) |
| 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 {{{BookAdmin}}} class and pass that class as a second argument to {{{admin.site.register()}}}. |
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): |
| 119 | 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: |
| 120 | |
| 121 | {{{ |
| 122 | #!python |
| 123 | |
| 124 | class BookAdmin(admin.ModelAdmin): |
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.) |
| 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 a ```queryset``` method: |
| 152 | |
| 153 | {{{ |
| 154 | #!python |
| 155 | |
| 156 | class 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 | }}} |
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: |
| 233 | class MyModelAdmin(admin.ModelAdmin): |
| 234 | fields = ('field1', 'field2', 'field3', 'field4') # Renaming is optional |
| 235 | |
| 236 | class AnotherModelAdmin(admin.ModelAdmin): |
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 | ) |