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, mo...
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)
    slu...
What has changed?

• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at applicati...
# admin.py
from django.contrib import admin
from myproject.myapps.models import Article

class ArticleAdmin(admin.ModelAdm...
What has changed?

• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at applicati...
# admin.py
from django.contrib import admin
from myproject.myapps.models import Article

class ArticleAdmin(admin.ModelAdm...
What has changed?
• No longer specify admin options in your
  model or model fields.
• Use an admin.py module at applicatio...
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = pa...
edit_inline change
• edit_inline has been completely refactored.
• Inlines now subclass InlineModelAdmin.
• InlineModelAdm...
# models.py
from django.db import models

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

    clas...
# admin.py
from django.contrib import admin
from myproject.myapps.models import Author, Book

class BookInline(admin.Stack...
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...
# forms.py
from django import forms
from django.forms.formsets import formset_factory

class BookForm(forms.Form):
    nam...
# views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from myproject.myapp...
{# 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...
Generic Inlines
• Inlines based on a generic foreign key.
• Allows for a reusable inline that can be
  applied generically...
# models.py
from django.db import models
from django.contrib.contenttypes import genric
from django.contrib.contenttypes.m...
# admin.py
from django.contrib.contenttypes import generic
from myproject.myapp.models import Image

class ImageInline(gen...
Media definitions
• Define media on forms and widgets.
• Allows for simple reusable widgets.
• Admin widgets use this so the...
class CalendarWidget(forms.TextInput):
    class Media:
        css = {‘all’: (‘pretty.css’,)}
        js = (‘animations.j...
>>> w = CalendarWidget()
>>> print w.media
<link href=’http://media.example.com/pretty.css’
type=’text/css’ media=’all’ re...
And one more thing

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

• http://github.com/jezdez/django-
  mobileadmin/
• http://docs.djangoproject.com/en/dev/ref/
  contrib/admin/
...
Questions?
What’S New In Newforms Admin
Upcoming SlideShare
Loading in...5
×

What’S New In Newforms Admin

2,260

Published on

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,260
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

What’S New In Newforms Admin

  1. 1. What’s New in newforms-admin Brian Rosner DjangoCon 2008
  2. 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. 3. What has changed? • No longer specify admin options in your model or model fields.
  4. 4. # 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’,)
  5. 5. 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.
  6. 6. # admin.py from django.contrib import admin from myproject.myapps.models import Article class ArticleAdmin(admin.ModelAdmin): list_display = (‘title’,) prepopulated_fields = {‘slug’: (‘title’,)}
  7. 7. 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.
  8. 8. # 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)
  9. 9. 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.
  10. 10. # urls.py from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns(‘’, url(r’^admin/(.*)’, admin.site.root), )
  11. 11. 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.
  12. 12. # 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)
  13. 13. # 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)
  14. 14. TabularInline
  15. 15. What is new? • Formsets • Model formsets • Generic Inlines • Media definitions on forms and widgets
  16. 16. 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
  17. 17. # forms.py from django import forms from django.forms.formsets import formset_factory class BookForm(forms.Form): name = forms.CharField() BookFormSet = formset_factory(BookForm)
  18. 18. # 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)
  19. 19. {# books.html #} <form method=”POST” action=””> {% for form in formset.forms %} {{ form }} {% endfor %} </form>
  20. 20. Model formsets • Helpers for integration with models. • BaseModelFormSet and BaseInlineFormSet • modelformset_factory and inlineformset_factory
  21. 21. 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
  22. 22. # 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’)
  23. 23. # 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’
  24. 24. 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
  25. 25. class CalendarWidget(forms.TextInput): class Media: css = {‘all’: (‘pretty.css’,)} js = (‘animations.js’, ‘actions.js’)
  26. 26. >>> 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>
  27. 27. And one more thing • Formsets need to determine if a widget has changed data. • Uses form.has_changed and form.changed_data.
  28. 28. Resources • http://github.com/jezdez/django- mobileadmin/ • http://docs.djangoproject.com/en/dev/ref/ contrib/admin/ • http://oebfare.com
  29. 29. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×