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

Django Admin: Pushing the Pony's boundries (Dja...

Django Admin: Pushing the Pony's boundries (DjangoCon US 2016)

Ola Sitarska

July 18, 2016
Tweet

More Decks by Ola Sitarska

Other Decks in Technology

Transcript

  1. DjangoCon Europe 2013, 2015, 2016 Django Girls Django Core Team

    DSF Code of Conduct Django Under The Hood Senior Django Dev @ Potato
  2. class AdminSite(object): … @property def get_urls(self): # Admin-site-wide urls urlpatterns

    = [ url(r'^$', wrap(self.index), name='index'), url(r'^login/$', self.login, name='login'), url(r'^logout/$', wrap(self.logout), 
 name='logout'), … AdminSite: URLs AdminSite: URLs
  3. class AdminSite(object): … @property def get_urls(self): # Admin-site-wide urls urlpatterns

    = [ url(r'^$', wrap(self.index), name='index'), url(r'^login/$', self.login, name='login'), url(r'^logout/$', wrap(self.logout), 
 name='logout'), … AdminSite: URLs
  4. # Add in each model's views, and create a list

    of 
 # valid URLS for the app_index for model, model_admin in self._registry.items(): urlpatterns += [ url(r'^%s/%s/' % (model._meta.app_label, 
 model._meta.model_name), 
 include(model_admin.urls) ), ] AdminSite: URLs
  5. # Add in each model's views, and create a list

    of 
 # valid URLS for the app_index for model, model_admin in self._registry.items(): urlpatterns += [ url(r'^%s/%s/' % (model._meta.app_label, 
 model._meta.model_name), 
 include(model_admin.urls) ), ] AdminSite: URLs
  6. # Add in each model's views, and create a list

    of 
 # valid URLS for the app_index for model, model_admin in self._registry.items(): urlpatterns += [ url(r'^%s/%s/' % (model._meta.app_label, 
 model._meta.model_name), 
 include(model_admin.urls) ), ] AdminSite: URLs
  7. # Add in each model's views, and create a list

    of 
 # valid URLS for the app_index for model, model_admin in self._registry.items(): urlpatterns += [ url(r'^%s/%s/' % (model._meta.app_label, 
 model._meta.model_name), 
 include(model_admin.urls) ), ] AdminSite: URLs
  8. AdminSite: model registry from django.contrib import admin admin.site.register(Author, AuthorAdmin) @admin.register(Author)

    class AuthorAdmin(admin.ModelAdmin): admin.site.register(Author) register(Author, list_display=(‘name’, ‘age’))
  9. AdminSite: basic config class AdminSite(object): # Text to put at

    the end of each page's <title>. site_title = ugettext_lazy('Django site admin') # Text to put in each page's <h1>. site_header = ugettext_lazy('Django administration') # Text to put at the top of the admin index page. index_title = ugettext_lazy('Site administration')
  10. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  11. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  12. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  13. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  14. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  15. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  16. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  17. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  18. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  19. Advanced permissions class EventAdmin(admin.ModelAdmin): list_display = ('name', 'organizers', ‘date') search_fields

    = ('city', 'country', 'name') def get_queryset(self, request): qs = super(EventAdmin, self).queryset(request) if request.user.is_superuser: return qs return qs.filter(team=request.user)
  20. Advanced permissions class EventAdmin(admin.ModelAdmin): list_display = ('name', 'organizers', ‘date') search_fields

    = ('city', 'country', 'name') def get_queryset(self, request): qs = super(EventAdmin, self).queryset(request) if request.user.is_superuser: return qs return qs.filter(team=request.user)
  21. Advanced permissions class EventAdmin(admin.ModelAdmin): list_display = ('name', 'organizers', ‘date') search_fields

    = ('city', 'country', 'name') def get_queryset(self, request): qs = super(EventAdmin, self).queryset(request) if request.user.is_superuser: return qs return qs.filter(team=request.user)
  22. Advanced permissions class EventAdmin(admin.ModelAdmin): list_display = ('name', 'organizers', ‘date') search_fields

    = ('city', 'country', 'name') def get_queryset(self, request): qs = super(EventAdmin, self).queryset(request) if request.user.is_superuser: return qs return qs.filter(team=request.user)
  23. Advanced permissions def get_form(self, request, obj=None, **kwargs): form = super(EventPageContentAdmin,

    self).get_form(request, obj, **kwargs) if not request.user.is_superuser: if 'page' in form.base_fields: form.base_fields['page'].queryset = \ EventPage.objects.filter( event__team=request.user ) return form
  24. Advanced permissions def get_form(self, request, obj=None, **kwargs): form = super(EventPageContentAdmin,

    self).get_form(request, obj, **kwargs) if not request.user.is_superuser: if 'page' in form.base_fields: form.base_fields['page'].queryset = \ EventPage.objects.filter( event__team=request.user ) return form
  25. Advanced permissions def get_form(self, request, obj=None, **kwargs): form = super(EventPageContentAdmin,

    self).get_form(request, obj, **kwargs) if not request.user.is_superuser: if 'page' in form.base_fields: form.base_fields['page'].queryset = \ EventPage.objects.filter( event__team=request.user ) return form
  26. Advanced permissions def get_form(self, request, obj=None, **kwargs): form = super(EventPageContentAdmin,

    self).get_form(request, obj, **kwargs) if not request.user.is_superuser: if 'page' in form.base_fields: form.base_fields['page'].queryset = \ EventPage.objects.filter( event__team=request.user ) return form
  27. Advanced permissions • organizers can edit only their own events

    • organizers can edit only future
 events
  28. ModelAdmin actions actions_on_top actions_on_bottom actions_selection_counter date_hierarchy exclude fields fieldsets filter_horizontal

    form formfield_overrides inlines list_display list_display_links list_editable list_filter list_max_show_all list_per_page list_select_related ordering paginator prepopulated_fields preserve_filters radio_fields raw_id_fields readonly_fields save_as save_on_top search_fields show_full_result_count view_on_site
  29. Advanced permissions def get_readonly_fields(self, request, obj=None): if obj and not

    request.user.is_superuser: if not obj.page.event.is_upcoming(): return set( [x.name for x in self.model._meta.fields] ) return self.readonly_fields
  30. Advanced permissions def get_readonly_fields(self, request, obj=None): if obj and not

    request.user.is_superuser: if not obj.page.event.is_upcoming(): return set( [x.name for x in self.model._meta.fields] ) return self.readonly_fields
  31. Advanced permissions def get_readonly_fields(self, request, obj=None): if obj and not

    request.user.is_superuser: if not obj.page.event.is_upcoming(): return set( [x.name for x in self.model._meta.fields] ) return self.readonly_fields
  32. Advanced permissions def get_readonly_fields(self, request, obj=None): if obj and not

    request.user.is_superuser: if not obj.page.event.is_upcoming(): return set( [x.name for x in self.model._meta.fields] ) return self.readonly_fields
  33. Advanced permissions def get_readonly_fields(self, request, obj=None): if obj and not

    request.user.is_superuser: if not obj.page.event.is_upcoming(): return set( [x.name for x in self.model._meta.fields] ) return self.readonly_fields
  34. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_organizers’, ‘date')

    … def get_organizers(self, obj): members = [] for member in self.team.all(): members.append( u’{name} <{email}>’.format( member.get_full_name(), member.email ) ) return ', '.join(members) get_organizers.short_description = “Organizers”
  35. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_organizers’, ‘date')

    … def get_organizers(self, obj): members = [] for member in self.team.all(): members.append( u’{name} <{email}>’.format( member.get_full_name(), member.email ) ) return ', '.join(members) get_organizers.short_description = “Organizers”
  36. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_organizers’, ‘date')

    … def get_organizers(self, obj): members = [] for member in self.team.all(): members.append( u’{name} <{email}>’.format( member.get_full_name(), member.email ) ) return ', '.join(members) get_organizers.short_description = “Organizers”
  37. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_organizers’, ‘date')

    … def get_organizers(self, obj): members = [] for member in self.team.all(): members.append( u’{name} <{email}>’.format( member.get_full_name(), member.email ) ) return ', '.join(members) get_organizers.short_description = “Organizers”
  38. Custom method fields • display all organizers as list on

    ChangeList view • display status of the event
  39. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘is_event_over’, ‘date')

    … def is_event_over(self, obj): return not obj.is_upcoming() is_event_over.boolean = True
  40. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘is_event_over’, ‘date')

    … def is_event_over(self, obj): return not obj.is_upcoming() is_event_over.boolean = True
  41. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘is_event_over’, ‘date')

    … def is_event_over(self, obj): return not obj.is_upcoming() is_event_over.boolean = True
  42. Custom method fields • display all organizers as list on

    ChangeList view • display status of the event • display images on a list in the ChangeList view
  43. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date')

    … def get_logo_display(self, obj): if obj.logo: return '<a href="{url}" target="_blank"><img src="{url}" width="100" /></a>'.format(self.logo.url) else: return 'No logo!' get_logo_display.allow_tags = True
  44. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date')

    … def get_logo_display(self, obj): if obj.logo: return '<a href="{url}" target="_blank"><img src="{url}" width="100" /></a>'.format(self.logo.url) else: return 'No logo!' get_logo_display.allow_tags = True
  45. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date')

    … def get_logo_display(self, obj): if obj.logo: return '<a href="{url}" target="_blank"><img src="{url}" width="100" /></a>'.format(self.logo.url) else: return 'No logo!' get_logo_display.allow_tags = True
  46. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date')

    … def get_logo_display(self, obj): if obj.logo: return '<a href="{url}" target="_blank"><img src="{url}" width="100" /></a>'.format(self.logo.url) else: return 'No logo!' get_logo_display.allow_tags = True
  47. Custom method fields class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date')

    … def get_logo_display(self, obj): if obj.logo: return '<a href="{url}" target="_blank"><img src="{url}" width="100" /></a>'.format(self.logo.url) else: return 'No logo!' get_logo_display.allow_tags = True
  48. Custom actions class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date’) actions

    = [‘show_on_homepage’] … def show_on_homepage(self, request, queryset): queryset.update(is_on_homepage=True) show_on_homepage.short_description = "Publish selected”
  49. class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date’) actions = [‘show_on_homepage’]

    … def show_on_homepage(self, request, queryset): queryset.update(is_on_homepage=True) show_on_homepage.short_description = "Publish selected” Custom actions
  50. class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date’) actions = [‘show_on_homepage’]

    … def show_on_homepage(self, request, queryset): queryset.update(is_on_homepage=True) show_on_homepage.short_description = "Publish selected” Custom actions
  51. class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date’) actions = [‘show_on_homepage’]

    … def show_on_homepage(self, request, queryset): queryset.update(is_on_homepage=True) show_on_homepage.short_description = "Publish selected” Custom actions
  52. class EventAdmin(admin.ModelAdmin): list_display = ('name', ‘get_logo_display’, ‘date’) actions = [‘show_on_homepage’]

    … def show_on_homepage(self, request, queryset): queryset.update(is_on_homepage=True) show_on_homepage.short_description = "Publish selected” Custom actions