what's new in newforms-admin
DESCRIPTION
The "What's New in newforms-admin" presentation I gave at DjangoCon 2008.TRANSCRIPT
![Page 1: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/1.jpg)
What’s New in newforms-admin
Brian RosnerDjangoCon 2008
![Page 2: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/2.jpg)
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.
![Page 3: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/3.jpg)
![Page 4: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/4.jpg)
What has changed?
• No longer specify admin options in your model or model fields.
![Page 5: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/5.jpg)
# models.pyfrom 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’,)
![Page 6: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/6.jpg)
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.
![Page 7: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/7.jpg)
# admin.pyfrom django.contrib import adminfrom myproject.myapps.models import Article
class ArticleAdmin(admin.ModelAdmin): list_display = (‘title’,) prepopulated_fields = {‘slug’: (‘title’,)}
![Page 8: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/8.jpg)
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.
![Page 9: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/9.jpg)
# admin.pyfrom django.contrib import adminfrom myproject.myapps.models import Article
class ArticleAdmin(admin.ModelAdmin): list_display = (‘title’,) prepopulated_fields = {‘slug’: (‘title’,)}
admin.site.register(Article, ArticleAdmin)
![Page 10: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/10.jpg)
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.
![Page 11: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/11.jpg)
# urls.pyfrom django.conf.urls.defaults import *from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns(‘’, url(r’^admin/(.*)’, admin.site.root),)
![Page 12: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/12.jpg)
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.
![Page 13: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/13.jpg)
# models.pyfrom 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)
![Page 14: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/14.jpg)
# admin.pyfrom django.contrib import adminfrom myproject.myapps.models import Author, Book
class BookInline(admin.StackedInline): model = Book
class AuthorAdmin(admin.ModelAdmin): inlines = [BookInline]
admin.site.register(Author, AuthorAdmin)
![Page 15: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/15.jpg)
TabularInline
![Page 16: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/16.jpg)
What is new?
• Formsets
• Model formsets
• Generic Inlines
• Media definitions on forms and widgets
![Page 17: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/17.jpg)
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
![Page 18: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/18.jpg)
# forms.pyfrom django import formsfrom django.forms.formsets import formset_factory
class BookForm(forms.Form): name = forms.CharField()
BookFormSet = formset_factory(BookForm)
![Page 19: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/19.jpg)
# views.pyfrom django.shortcuts import render_to_responsefrom django.template import RequestContextfrom 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)
![Page 20: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/20.jpg)
{# books.html #}<form method=”POST” action=””>{% for form in formset.forms %} {{ form }}{% endfor %}</form>
![Page 21: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/21.jpg)
Model formsets
• Helpers for integration with models.
• BaseModelFormSet and BaseInlineFormSet
• modelformset_factory and inlineformset_factory
![Page 22: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/22.jpg)
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
![Page 23: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/23.jpg)
# models.pyfrom django.db import modelsfrom django.contrib.contenttypes import genricfrom 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’)
![Page 24: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/24.jpg)
# admin.pyfrom django.contrib.contenttypes import genericfrom myproject.myapp.models import Image
class ImageInline(generic.GenericInlineModelAdmin): model = Image ct_field = ‘content_type’ ct_fk_field = ‘object_pk’
![Page 25: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/25.jpg)
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
![Page 26: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/26.jpg)
class CalendarWidget(forms.TextInput): class Media: css = {‘all’: (‘pretty.css’,)} js = (‘animations.js’, ‘actions.js’)
![Page 27: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/27.jpg)
>>> 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>
![Page 28: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/28.jpg)
And one more thing
• Formsets need to determine if a widget has changed data.
• Uses form.has_changed and form.changed_data.
![Page 29: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/29.jpg)
Resources
• http://github.com/jezdez/django-mobileadmin/
• http://docs.djangoproject.com/en/dev/ref/contrib/admin/
• http://oebfare.com
![Page 30: What's New in newforms-admin](https://reader031.vdocument.in/reader031/viewer/2022020306/54b7b7ff4a7959c9688b473c/html5/thumbnails/30.jpg)
Questions?