Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Customizing the Django Admin: The how ad the wh...

PyCon 2013
March 16, 2013

Customizing the Django Admin: The how ad the why by Lakshman Prasad

PyCon 2013

March 16, 2013
Tweet

More Decks by PyCon 2013

Other Decks in Technology

Transcript

  1. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Customizing the Django Admin: The How and the Why. Lakshman Prasad (twitter.com/becomingGuru)
  2. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    ”Nothing on the web from five years ago is still awesome by today’s standards. Nothing.” – Jeff Croft
  3. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Admin Issues Setting Perspective Overview Customizations External apps Admin Next
  4. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Import the model, override model admin, specify fields from . models import TransferEarnings from . forms import TransferEarningsForm from django . admin import ModelAdmin class TransferEarningsAdmin ( ModelAdmin ) : l i s t p e r p a g e = 15 s e a r c h f i e l d s = [ ’ u s e r l o g i n i d ’ ] l i s t d i s p l a y = ( ’ user ’ , ’ date ’ , ’ amount ’ , ’ t r a n s f e r t o ’ f i e l d s = ( ’ user ’ , ’ amount ’ , ’ date ’ , ’ t r a n s f e r t o ’ ) readonly = ( ’ user ’ , ’ amount ’ , ’ t r a n s f e r t o ’ , ’ date ’ ) form = TransferEarningsForm l i s t f i l t e r = [ ’ s t a t e ’ ]
  5. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Auto discover the admin modules # u r l s . py from django . conf . u r l s import patterns , url , i n c l u d e from django . c o n t r i b import admin admin . a u t o d i s c o v e r () u r l p a t t e r n s = p a t t e r n s ( ’ ’ , ( r ’ ˆadmin/ ’ , i n c l u d e ( admin . s i t e . u r l s )) , )
  6. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    list filter enables filtering, Actions enable actions on models
  7. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Custom class list filters c l a s s AdvancedDecadeBornListFilter ( django . c o n t r i b . admin . S i m p l e L i s t F i l t e r ) : def lookups ( s e l f , request , model admin ) : ””” Only show the lookups i f t h e r e a c t u a l l y i s anyone born i n the c o r r e s p o n d i n g decades . ””” qs = model admin . q u e r y s e t ( r e q u e s t ) i f qs . f i l t e r ( b i r t h d a y g t e=date (1980 , 1 , 1) , b i r t h d a y l t e=date (1989 , 12 , 3 1 ) ) . e x i s t s ( ) : y i e l d ( ’ 80 s ’ , ( ’ i n the e i g h t i e s ’ )) i f qs . f i l t e r ( b i r t h d a y g t e=date (1990 , 1 , 1) , b i r t h d a y l t e=date (1999 , 12 , 3 1 ) ) . e x i s t s ( ) : y i e l d ( ’ 90 s ’ , ( ’ i n the n i n e t i e s ’ )) def q u e r y s e t ( s e l f , request , q u e r y s e t ) :
  8. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Custom admin actions class ArticleAdmin ( admin . ModelAdmin ) : . . . a c t i o n s = [ ’ make published ’ ] def make published ( s e l f , request , queryset ) : queryset . update ( s t a t u s=’p ’ ) make published . s h o r t d e s c r i p t i o n = \ ”Mark s e l e c t e d s t o r i e s as p u b l i s h e d ”
  9. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Can be overriden per app/model in templates/admin/myapp/ app index . html change form . html c h a n g e l i s t . html d e l e t e c o n f i r m a t i o n . html o b j e c t h i s t o r y . html
  10. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    templates/admin/myapp/changeform.html {% extends ”admin/ change form . html ” %} {% load i18n %} {% block object−t o o l s %} {% i f change %}{% i f not is popup %} <u l c l a s s=” object−t o o l s ”> <l i > <a h r e f=” h i s t o r y /” c l a s s=” h i s t o r y l i n k ”>{% t r a n s ” H i s t o r y ” %}</a> </l i > <l i > <a h r e f=” mylink /” c l a s s=” h i s t o r y l i n k ”>My Link</a> </l i > {% i f h a s a b s o l u t e u r l %} <l i > <a h r e f=” . . / . . / . . / r /{{ c o n t e n t t y p e i d }}/{{ o b j e c t i d }}/” c l a s s=” v i e w s i t e l i n k ”> {% t r a n s ”View on s i t e ” %}</a> </l i > {% e n d i f%} </ul> {% e n d i f %}{% e n d i f %} {% endblock %}
  11. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Custom media definitions class ArticleAdmin ( admin . ModelAdmin ) : class Media : css = { ” a l l ” : ( ” m y s t y l e s . css ” ,) } j s = ( ”my code . j s ” ,)
  12. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Row level permissions has add permission ( s e l f , request ) has change permission ( s e l f , request , obj=None ) h a s d e l e t e p e r m i s s i o n ( s e l f , request , obj=None )
  13. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Explicitly add the other admin modules # u r l s . py from django . conf . u r l s import patterns , url , i n c l u d e from myproject . admin import b a s i c s i t e , a d v a n c e d s i t e u r l p a t t e r n s = p a t t e r n s ( ’ ’ , ( r ’ ˆ basic −admin/ ’ , i n c l u d e ( b a s i c s i t e . u r l s )) , ( r ’ ˆadvanced−admin/ ’ , i n c l u d e ( a d v a n c e d s i t e . u r l s )) , )
  14. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    • Fieldsets • Filter Horizontal • Tabular/Stacked Inlines • Generic Inlines • Form field overrides • Date Hierarchy • Raw ID fields • ReadOnly fields • Admin Docs
  15. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    Image Attributions http ://www. f l i c k r . com/ photos / h i n k e l s t o n e /2765597758 http ://www. f l i c k r . com/ photos /25521149@N03/2491915744 http ://www. f l i c k r . com/ photos /50764322@N04/8378057370 http ://www. f l i c k r . com/ photos /micahdowty /3866958782 http ://www. f l i c k r . com/ photos /81512202@N05/7468442446 http ://www. f l i c k r . com/ photos / marlenek /2051394370 http ://www. f l i c k r . com/ photos / t e j e d o r o d e l u z /3157690060/ http ://www. f l i c k r . com/ photos /23820645@N05/4287681570/ http ://www. f l i c k r . com/ photos / a i d a n j o n e s /3575000735/ http ://www. f l i c k r . com/ photos /go /253819/ http ://www. f l i c k r . com/ photos / o r i n r o b e r t j o h n /114430223/
  16. Admin Issues Setting Perspective Overview Customizations External apps Admin Next

    About Me • Active Djangonaut and active in Python world • Part of a few popular open source django applications github.com/becomingGuru, stackoverflow.com/users/55562 • Co-Authored an ebook ”django-design-patterns” • Architect and develop full stack applications at Mckinsey • Earlier, Consulting and Development via Agiliq Solutions • Developed several custom proprietory django applications • twitter.com/becomingGuru