Thursday, December 13, 2012

Chapter 6 Admin Panel


The django.contrib packages


CONFIGURATION for admin app
First, make a few changes to your settings file:
  1. Add 'django.contrib.admin' to the INSTALLED_APPS setting. (The order of INSTALLED_APPS doesn’t matter, but we like to keep things alphabetical so it’s easy for a human to read.)
  2. Make sure INSTALLED_APPS contains 'django.contrib.auth''django.contrib.contenttypes' and 'django.contrib.sessions'. The Django admin site requires these three packages. (If you’re following along with our ongoing mysite project, note that we commented out these three INSTALLED_APPS  entries in Chapter 5. Uncomment them now.)
  3. Make sure MIDDLEWARE_CLASSES contains 'django.middleware.common.CommonMiddleware','django.contrib.sessions.middleware.SessionMiddleware' and 'django.contrib.auth.middleware.AuthenticationMiddleware'. (Again, if you’re following along, note that we commented them out in Chapter 5, so uncomment them.)

Put 'django.contrib.admin' in your INSTALLED_APPS setting in order to use the admin application.s


 run python manage.py syncdb



REGISTRATION
# Include these import statements...
from django.contrib import admin
admin.autodiscover()

# And include this URLpattern...
urlpatterns = patterns('',
    # ...
    (r'^admin/', include(admin.site.urls)),
    # ...
)


Adding Your Models to the Admin Site

Within the books directory (mysite/books) App Directory
from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

(need restart the server)
Beyond that, the Django admin site is just a Django application, with its own models, templates, views and URLpatterns. 


dbshell
manage.py dbshell 
(sqlite not supported, 'sqlite3' is not recognized as an internal or external command, operable program or batch file.)


list_display (by default: __unicode__())

change the display name in list in Django Adming

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')

admin.site.register(Publisher)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book)


class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('first_name', 'last_name')


Select author to change





class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'publisher', 'publication_date')
    list_filter = ('publication_date',)

Screenshot of the book change list page after list_filter.
to be continued when you do the customization of the Admin Panel

Table of Content (index)
 list_dispaly
list_filter
ordering
search_fileds # does not work with FK

The django.contrib packages

Activating the Admin Interface

Using the Admin Site

Adding Your Models to the Admin Site

How the Admin Site Works

Making Fields Optional

Making Date and Numeric Fields Optional

Customizing Field Labels

Custom ModelAdmin classes

Customizing change lists

Customizing edit forms

Users, Groups, and Permissions

When and Why to Use the Admin Interface – And When Not to




No comments:

Post a Comment