Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created September 18, 2011 02:11
Show Gist options
  • Select an option

  • Save mhulse/1224591 to your computer and use it in GitHub Desktop.

Select an option

Save mhulse/1224591 to your computer and use it in GitHub Desktop.
Django admin starter template (A.K.A. my attempt at organizing the Django's admin.py file).
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from <app>.models import Foo, Bar, Baz
from <app>.forms import BazForm
# https://docs.djangoproject.com/en/dev/ref/contrib/admin/
#--------------------------------------------------------------------------
#
# Classes:
#
#--------------------------------------------------------------------------
class BazChangeList(ChangeList):
# ...
pass
#--------------------------------------------------------------------------
#
# Methods:
#
#--------------------------------------------------------------------------
#----------------------------------
# Actions:
#----------------------------------
def some_action(modeladmin, request, queryset):
# ...
pass
#----------------------------------
# Changelist:
#----------------------------------
def baz_getchangelist(self, request, **kwargs):
return BazChangeList
#--------------------------------------------------------------------------
#
# Model inlines:
#
#--------------------------------------------------------------------------
class FooModelInline(admin.StackedInline):
# Stacked inline ...
#----------------------------------
# Standard options:
#----------------------------------
fields = ('name', 'photo', 'file', 'uri', 'embed', 'description',)
# form
# fieldsets
# exclude
# filter_horizontal
# filter_vertical
# prepopulated_fields
# radio_fields
# raw_id_fields
# formfield_for_foreignkey()
# formfield_for_manytomany()
# readonly_fields
# formfield_overrides
# ordering
# queryset()
#----------------------------------
# Inline-specific options:
#----------------------------------
model = Foo
extra = 1
# fk_name
# formset
# form
# max_num
# raw_id_fields
# template
# verbose_name
# verbose_name_plural
# can_delete
class BarModelInline(admin.TabularInline):
# Tabular inline ...
pass
#--------------------------------------------------------------------------
#
# Models:
#
#--------------------------------------------------------------------------
class BazAdmin(admin.ModelAdmin):
#----------------------------------
# Fields:
#----------------------------------
"""
fields = ('slug', 'name', 'notes',) # Basic.
fieldsets = (
(None, {
'fields': (('name', 'slug'), 'notes',), # In this example, "name" and "slug" are on same line.
}),
)
"""
fieldsets = [
('Meta', {'fields': ['slug', 'status',], 'classes': ['collapse',],},),
('About', {'fields': ['title', 'categories', 'description',],},),
('Marker', {'description': 'This is a fieldset description', 'fields': ['address1', 'address2', 'city', 'state', 'zip', 'country', 'geolocation', 'miles',],},),
]
prepopulated_fields = {
'slug': ['title',],
}
readonly_fields = ('geolocation',)
exclude = ('phonographer',)
#----------------------------------
# Interfaces:
#----------------------------------
filter_horizontal = ('categories',)
# filter_vertical = ('categories',)
# raw_id_fields = ('categories',)
# radio_fields = {'phonographer': admin.VERTICAL} # ... or HORIZONTAL
#----------------------------------
# Forms:
#----------------------------------
form = BazForm
formfield_overrides = { models.TextField: {'widget': forms.Textarea(attrs={'class':'ckeditor'})}, } # See media section below.
#----------------------------------
# Inlines:
#----------------------------------
inlines = [FooModelInline, BarModelInline]
#----------------------------------
# Change lists:
#----------------------------------
get_changelist = baz_getchangelist # Never used this before... Is this (a) correct usage?
list_display = ('title', 'status', 'geolocation',)
list_display_links = ('title',)
list_editable = ('status',)
list_filter = ('status', 'categories',)
list_max_show_all = 200
list_per_page = 100
list_select_related = False
ordering = ['-created',]
# paginator = ??? (need example)
search_fields = ('title', 'description', 'address1', 'address2', 'city', 'state', 'zip', 'country', 'miles',)
actions = [some_action,]
actions_on_top = True;
actions_on_bottom = False
actions_selection_counter = True
date_hierarchy = 'created'
#----------------------------------
# Change forms:
#----------------------------------
save_as = False
save_on_top = False
#----------------------------------
# Template overrides:
#----------------------------------
add_form_template = '<app>/admin/add_form.html'
change_form_template = '<app>/admin/change_form.html'
change_list_template = '<app>/admin/change_list.html'
delete_confirmation_template = '<app>/admin/delete_confirmation.html'
delete_selected_confirmation_template = '<app>/admin/delete_selected_confirmation.html'
object_history_template = '<app>/admin/object_history_template.html'
#----------------------------------
# Methods:
#----------------------------------
def save_model(self, request, obj, form, change):
pass
def delete_model(self, request, obj):
pass
def save_formset(self, request, form, formset, change):
pass
def get_ordering(self, request):
pass
def save_related(self, request, form, formsets, change):
pass
def get_readonly_fields(self, request, obj=None):
pass
def get_prepopulated_fields(self, request, obj=None):
pass
def get_list_display(self, request):
pass
def get_urls(self):
pass
def formfield_for_foreignkey(self, db_field, request, **kwargs):
pass
def formfield_for_manytomany(self, db_field, request, **kwargs):
pass
def formfield_for_choice_field(self, db_field, request, **kwargs):
pass
def has_add_permission(self, request):
pass
def has_change_permission(self, request, obj=None):
pass
def has_delete_permission(self, request, obj=None):
pass
def queryset(self, request):
pass
def message_user(request, message):
pass
def get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True):
pass
def add_view(self, request, form_url='', extra_context=None)
pass
def change_view(self, request, object_id, extra_context=None):
pass
def changelist_view(self, request, extra_context=None):
pass
def delete_view(self, request, object_id, extra_context=None):
pass
def history_view(self, request, object_id, extra_context=None):
pass
#----------------------------------
# Media definitions:
#----------------------------------
"""
Note: Django admin Javascript makes use of the jQuery library. To avoid conflict with user
scripts, Django's jQuery is namespaced as django.jQuery. If you want to use jQuery in your own
admin JavaScript without including a second copy, you can use the django.jQuery object on
changelist and add/edit views.
"""
class Media:
css = {
'all': ('<app>/my_styles.css',)
}
js = ('ckeditor/ckeditor.js',)
#--------------------------------------------------------------------------
#
# Registrations:
#
#--------------------------------------------------------------------------
admin.site.register(Baz, BazAdmin)
# Using the default admin interface:
admin.site.register(Foo)
admin.site.register(Bar)
from django.contrib import admin
from <app>.models import Foo, Bar
# https://docs.djangoproject.com/en/dev/ref/contrib/admin/
#--------------------------------------------------------------------------
#
# Classes:
#
#--------------------------------------------------------------------------
# ...
#--------------------------------------------------------------------------
#
# Methods:
#
#--------------------------------------------------------------------------
# ...
#--------------------------------------------------------------------------
#
# Model inlines:
#
#--------------------------------------------------------------------------
# class FooInline(admin.TabularInline):
class FooInline(admin.StackedInline):
#----------------------------------
# Standard options:
#----------------------------------
# fields
# form
# fieldsets
# exclude
# filter_horizontal
# filter_vertical
# prepopulated_fields
# radio_fields
# raw_id_fields
# formfield_for_foreignkey()
# formfield_for_manytomany()
# readonly_fields
# formfield_overrides
# ordering
# queryset()
#----------------------------------
# Inline-specific options:
#----------------------------------
model = Foo
# extra
# fk_name
# formset
# form
# max_num
# raw_id_fields
# template
# verbose_name
# verbose_name_plural
# can_delete
#--------------------------------------------------------------------------
#
# Models:
#
#--------------------------------------------------------------------------
class BarAdmin(admin.ModelAdmin):
#----------------------------------
# Fields:
#----------------------------------
# ...
#----------------------------------
# Interfaces:
#----------------------------------
# ...
#----------------------------------
# Forms:
#----------------------------------
# ...
#----------------------------------
# Inlines:
#----------------------------------
inlines = [FooInline,]
# ...
#----------------------------------
# Change lists:
#----------------------------------
# ...
#----------------------------------
# Change forms:
#----------------------------------
# ...
#----------------------------------
# Template overrides:
#----------------------------------
# ...
#----------------------------------
# Methods:
#----------------------------------
# ...
#----------------------------------
# Media definitions:
#----------------------------------
# ...
#--------------------------------------------------------------------------
#
# Registrations:
#
#--------------------------------------------------------------------------
admin.site.register(Foo)
admin.site.register(Bar, BarAdmin)
@mhulse
Copy link
Copy Markdown
Author

mhulse commented Sep 18, 2011

Discussion found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment