My Wiki!

Model

Resources

Simple Model

Admin Site

CRUD operations on model is provided with django-admin. This section explains how it works and how to modify default behaviors.

Create Admin User

  python manage.py createsuperuser
  

Register Model with Admin Site

Edit myapp/admin.py

from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)

Now it is possible to CRUD MyModel in Admin Site.

  • The form is automatically generated from the Question model.
  • The different model field types (DateTimeField, CharField) correspond to the appropriate HTML input widget. Each type of field knows how to display itself in the Django admin.
  • Each DateTimeField gets free JavaScript shortcuts. Dates get a “Today” shortcut and calendar popup, and times get a “Now” shortcut and a convenient popup that lists commonly entered times.

If the value of “Date published” doesn’t match the time when you created the question in Tutorial 1, it probably means you forgot to set the correct value for the TIME_ZONE setting. Change it, reload the page and check that the correct value appears.

Customize The Admin Form

Edit myapp/admin.py to use customized admin form the app instead of the auto generated. create a model admin object, then pass it as the second argument to admin.site.register() – any time you need to change the admin options for an object.

    from django.contrib import admin
    from .models import Question
 
    class QuestionAdmin(admin.ModelAdmin):
        fields = ['pub_date', 'question_text']
 
    admin.site.register(Question, QuestionAdmin)

The order in fileds[] will reflexes in Admin Site.

Splitting the fields in to fieldsets

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

Collapsed Fieldset

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]

This tells Django: “Choice objects are edited on the Question admin page. By default, provide enough fields for 3 choices.”

from django.contrib import admin
 
from .models import Choice, Question
 
class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3
 
 
class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]
 
admin.site.register(Question, QuestionAdmin)

Use other Inline class:

  class ChoiceInline(admin.TabularInline):
  

Customize Change List

By default, Django displays the str() of each object. But sometimes it’d be more helpful if we could display individual fields. To do that, use the list_display admin option, which is a tuple of field names to display, as columns, on the change list page for the object:

polls/admin.py
 
class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date')

Just for good measure, let’s also include the waspublishedrecently custom method from Tutorial 1:

class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date', 'was_published_recently')

Change the default display of waspublishedrecently:

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

More customization options: https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

Add these options to Adminobject

  list_filter = ['pub_date']
  search_fields = ['question_text']
  

Now’s also a good time to note that change lists give you free pagination. The default is to display 100 items per page. Change list pagination, search boxes, filters, date-hierarchies, and column-header-ordering all work together like you think they should.


Navigation