SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
What’s New in
newforms-admin
    Brian Rosner
   DjangoCon 2008
What is it?
• A branch of Django trunk that refactored
  django.contrib.admin to use newforms.
• Many API improvements, model decoupling
  and new features.
• Landed in trunk at revision 7967.
• A part of Django 1.0.
What has changed?


• No longer specify admin options in your
  model or model fields.
# models.py
from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=50)
    slug = models.SlugField(
        prepopulate_fields=(‘title’,))

    class Admin:
        list_display = (‘title’,)
What has changed?

• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at application level
  to define your ModelAdmin classes.
# admin.py
from django.contrib import admin
from myproject.myapps.models import Article

class ArticleAdmin(admin.ModelAdmin):
    list_display = (‘title’,)
    prepopulated_fields = {‘slug’: (‘title’,)}
What has changed?

• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at application level
  to define your ModelAdmin classes.
• Register ModelAdmin classes to an
  AdminSite instance.
# admin.py
from django.contrib import admin
from myproject.myapps.models import Article

class ArticleAdmin(admin.ModelAdmin):
    list_display = (‘title’,)
    prepopulated_fields = {‘slug’: (‘title’,)}

admin.site.register(Article, ArticleAdmin)
What has changed?
• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at application level
  to define your ModelAdmin classes.
• Register ModelAdmin classes to an
  AdminSite instance.
• Hook up AdminSite instances in your
  URLconf.
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns(‘’,
    url(r’^admin/(.*)’, admin.site.root),
)
edit_inline change
• edit_inline has been completely refactored.
• Inlines now subclass InlineModelAdmin.
• InlineModelAdmin inherits from
  BaseModelAdmin.
• Django provides StackedInline and
  TabularInline out of the box.
# models.py
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=50)

    class Admin:
        pass

class Book(models.Model):
    author = models.ForeignKey(Author,
        edit_inline=models.STACKED)
    title = models.CharField(max_length=50,
        core=True)
# admin.py
from django.contrib import admin
from myproject.myapps.models import Author, Book

class BookInline(admin.StackedInline):
    model = Book

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline]

admin.site.register(Author, AuthorAdmin)
TabularInline
What is new?

• Formsets
• Model formsets
• Generic Inlines
• Media definitions on forms and widgets
Formsets

• Provides a simple API for handling more
  than one like form on the same page.
• The underlying implementation of inlines.
• django.forms.formsets.formset_factory
# forms.py
from django import forms
from django.forms.formsets import formset_factory

class BookForm(forms.Form):
    name = forms.CharField()

BookFormSet = formset_factory(BookForm)
# views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from myproject.myapp.forms import BookFormSet

def books(request):
    if request.method == ‘POST’:
        formset = BookFormSet(request.POST)
        if formset.is_valid():
            # do something
    else:
        formset = BookFormSet()
    return render_to_response(‘books.html’, {
        ‘formset’: formset,
    }, context_instance=RequestContext(request)
{# books.html #}
<form method=”POST” action=””>
{% for form in formset.forms %}
    {{ form }}
{% endfor %}
</form>
Model formsets

• Helpers for integration with models.
• BaseModelFormSet and BaseInlineFormSet
• modelformset_factory and
  inlineformset_factory
Generic Inlines
• Inlines based on a generic foreign key.
• Allows for a reusable inline that can be
  applied generically to ModelAdmin classes.
• GenericInlineModelAdmin
• GenericTabularInline and
  GenericStackedInline
# models.py
from django.db import models
from django.contrib.contenttypes import genric
from django.contrib.contenttypes.models import *

class Image(models.Model):
    image = models.ImageField(upload_to=’images’)

    object_pk = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    content_object = GenericForeignKey(
        ‘content_type’, ‘object_pk’)
# admin.py
from django.contrib.contenttypes import generic
from myproject.myapp.models import Image

class ImageInline(generic.GenericInlineModelAdmin):
    model = Image
    ct_field = ‘content_type’
    ct_fk_field = ‘object_pk’
Media definitions
• Define media on forms and widgets.
• Allows for simple reusable widgets.
• Admin widgets use this so they can be used
  outside the admin.
• Applies to ModelAdmin and
  InlineModelAdmin
class CalendarWidget(forms.TextInput):
    class Media:
        css = {‘all’: (‘pretty.css’,)}
        js = (‘animations.js’, ‘actions.js’)
>>> w = CalendarWidget()
>>> print w.media
<link href=’http://media.example.com/pretty.css’
type=’text/css’ media=’all’ rel=’stylesheet />
<script type=’text/javascript’ src=’http://
media.example.com/animatation.js’></script>
<script type=’text/javascript’ src=’http://
media.example.com/actions.js’></script>
And one more thing

• Formsets need to determine if a widget has
  changed data.
• Uses form.has_changed and
  form.changed_data.
Resources

• http://github.com/jezdez/django-
  mobileadmin/
• http://docs.djangoproject.com/en/dev/ref/
  contrib/admin/
• http://oebfare.com
Questions?

Contenu connexe

Tendances (6)

Two scoopsofdjango ch16 dealing with the user model
Two scoopsofdjango ch16   dealing with the user modelTwo scoopsofdjango ch16   dealing with the user model
Two scoopsofdjango ch16 dealing with the user model
 
Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16
 
CSS in React - Will Change Transform
CSS in React - Will Change TransformCSS in React - Will Change Transform
CSS in React - Will Change Transform
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating views
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & FiltersGravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
 
Rails view chapte 5 - form
Rails view   chapte 5 - formRails view   chapte 5 - form
Rails view chapte 5 - form
 

Similaire à What’S New In Newforms Admin

Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
Oro Inc.
 
Django talk
Django talkDjango talk
Django talk
psibi
 
A gentle intro to the Django Framework
A gentle intro to the Django FrameworkA gentle intro to the Django Framework
A gentle intro to the Django Framework
Ricardo Soares
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
 
Customizing the Django Admin
Customizing the Django AdminCustomizing the Django Admin
Customizing the Django Admin
Lincoln Loop
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
fool2nd
 

Similaire à What’S New In Newforms Admin (20)

What's New in newforms-admin
What's New in newforms-adminWhat's New in newforms-admin
What's New in newforms-admin
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django cheat sheet
Django cheat sheetDjango cheat sheet
Django cheat sheet
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
 
Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
 
Django talk
Django talkDjango talk
Django talk
 
Django
DjangoDjango
Django
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Django cms best practices
Django cms best practicesDjango cms best practices
Django cms best practices
 
A gentle intro to the Django Framework
A gentle intro to the Django FrameworkA gentle intro to the Django Framework
A gentle intro to the Django Framework
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
Django crush course
Django crush course Django crush course
Django crush course
 
DJango
DJangoDJango
DJango
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Customizing the Django Admin
Customizing the Django AdminCustomizing the Django Admin
Customizing the Django Admin
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 

Plus de DjangoCon2008

Plus de DjangoCon2008 (6)

Why I Hate Django
Why I Hate DjangoWhy I Hate Django
Why I Hate Django
 
Pinax
PinaxPinax
Pinax
 
Satchmo
SatchmoSatchmo
Satchmo
 
Reusable Apps
Reusable AppsReusable Apps
Reusable Apps
 
High Performance Django 1
High Performance Django 1High Performance Django 1
High Performance Django 1
 
High Performance Django
High Performance DjangoHigh Performance Django
High Performance Django
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

What’S New In Newforms Admin

  • 1. What’s New in newforms-admin Brian Rosner DjangoCon 2008
  • 2. What is it? • A branch of Django trunk that refactored django.contrib.admin to use newforms. • Many API improvements, model decoupling and new features. • Landed in trunk at revision 7967. • A part of Django 1.0.
  • 3.
  • 4. What has changed? • No longer specify admin options in your model or model fields.
  • 5. # models.py from django.db import models class Article(models.Model): title = models.CharField(max_length=50) slug = models.SlugField( prepopulate_fields=(‘title’,)) class Admin: list_display = (‘title’,)
  • 6. What has changed? • No longer specify admin options in your model or model fields. • Use an admin.py module at application level to define your ModelAdmin classes.
  • 7. # admin.py from django.contrib import admin from myproject.myapps.models import Article class ArticleAdmin(admin.ModelAdmin): list_display = (‘title’,) prepopulated_fields = {‘slug’: (‘title’,)}
  • 8. What has changed? • No longer specify admin options in your model or model fields. • Use an admin.py module at application level to define your ModelAdmin classes. • Register ModelAdmin classes to an AdminSite instance.
  • 9. # admin.py from django.contrib import admin from myproject.myapps.models import Article class ArticleAdmin(admin.ModelAdmin): list_display = (‘title’,) prepopulated_fields = {‘slug’: (‘title’,)} admin.site.register(Article, ArticleAdmin)
  • 10. What has changed? • No longer specify admin options in your model or model fields. • Use an admin.py module at application level to define your ModelAdmin classes. • Register ModelAdmin classes to an AdminSite instance. • Hook up AdminSite instances in your URLconf.
  • 11. # urls.py from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns(‘’, url(r’^admin/(.*)’, admin.site.root), )
  • 12. edit_inline change • edit_inline has been completely refactored. • Inlines now subclass InlineModelAdmin. • InlineModelAdmin inherits from BaseModelAdmin. • Django provides StackedInline and TabularInline out of the box.
  • 13. # models.py from django.db import models class Author(models.Model): name = models.CharField(max_length=50) class Admin: pass class Book(models.Model): author = models.ForeignKey(Author, edit_inline=models.STACKED) title = models.CharField(max_length=50, core=True)
  • 14. # admin.py from django.contrib import admin from myproject.myapps.models import Author, Book class BookInline(admin.StackedInline): model = Book class AuthorAdmin(admin.ModelAdmin): inlines = [BookInline] admin.site.register(Author, AuthorAdmin)
  • 16. What is new? • Formsets • Model formsets • Generic Inlines • Media definitions on forms and widgets
  • 17. Formsets • Provides a simple API for handling more than one like form on the same page. • The underlying implementation of inlines. • django.forms.formsets.formset_factory
  • 18. # forms.py from django import forms from django.forms.formsets import formset_factory class BookForm(forms.Form): name = forms.CharField() BookFormSet = formset_factory(BookForm)
  • 19. # views.py from django.shortcuts import render_to_response from django.template import RequestContext from myproject.myapp.forms import BookFormSet def books(request): if request.method == ‘POST’: formset = BookFormSet(request.POST) if formset.is_valid(): # do something else: formset = BookFormSet() return render_to_response(‘books.html’, { ‘formset’: formset, }, context_instance=RequestContext(request)
  • 20. {# books.html #} <form method=”POST” action=””> {% for form in formset.forms %} {{ form }} {% endfor %} </form>
  • 21. Model formsets • Helpers for integration with models. • BaseModelFormSet and BaseInlineFormSet • modelformset_factory and inlineformset_factory
  • 22. Generic Inlines • Inlines based on a generic foreign key. • Allows for a reusable inline that can be applied generically to ModelAdmin classes. • GenericInlineModelAdmin • GenericTabularInline and GenericStackedInline
  • 23. # models.py from django.db import models from django.contrib.contenttypes import genric from django.contrib.contenttypes.models import * class Image(models.Model): image = models.ImageField(upload_to=’images’) object_pk = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType) content_object = GenericForeignKey( ‘content_type’, ‘object_pk’)
  • 24. # admin.py from django.contrib.contenttypes import generic from myproject.myapp.models import Image class ImageInline(generic.GenericInlineModelAdmin): model = Image ct_field = ‘content_type’ ct_fk_field = ‘object_pk’
  • 25. Media definitions • Define media on forms and widgets. • Allows for simple reusable widgets. • Admin widgets use this so they can be used outside the admin. • Applies to ModelAdmin and InlineModelAdmin
  • 26. class CalendarWidget(forms.TextInput): class Media: css = {‘all’: (‘pretty.css’,)} js = (‘animations.js’, ‘actions.js’)
  • 27. >>> w = CalendarWidget() >>> print w.media <link href=’http://media.example.com/pretty.css’ type=’text/css’ media=’all’ rel=’stylesheet /> <script type=’text/javascript’ src=’http:// media.example.com/animatation.js’></script> <script type=’text/javascript’ src=’http:// media.example.com/actions.js’></script>
  • 28. And one more thing • Formsets need to determine if a widget has changed data. • Uses form.has_changed and form.changed_data.
  • 29. Resources • http://github.com/jezdez/django- mobileadmin/ • http://docs.djangoproject.com/en/dev/ref/ contrib/admin/ • http://oebfare.com