import datetime
from django import forms
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
class BetterAuthor(Author):
write_speed = models.IntegerField()
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
class Meta:
unique_together = (
('author', 'title'),
)
ordering = ['id']
def __unicode__(self):
return self.title
class BookWithCustomPK(models.Model):
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
def __unicode__(self):
return u'%s: %s' % (self.my_pk, self.title)
class Editor(models.Model):
name = models.CharField(max_length=100)
class BookWithOptionalAltEditor(models.Model):
author = models.ForeignKey(Author)
# Optional secondary author
alt_editor = models.ForeignKey(Editor, blank=True, null=True)
title = models.CharField(max_length=100)
class Meta:
unique_together = (
('author', 'title', 'alt_editor'),
)
def __unicode__(self):
return self.title
class AlternateBook(Book):
notes = models.CharField(max_length=100)
def __unicode__(self):
return u'%s - %s' % (self.title, self.notes)
class AuthorMeeting(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
created = models.DateField(editable=False)
def __unicode__(self):
return self.name
class CustomPrimaryKey(models.Model):
my_pk = models.CharField(max_length=10, primary_key=True)
some_field = models.CharField(max_length=100)
# models for inheritance tests.
class Place(models.Model):
name = models.CharField(max_length=50)
city = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Owner(models.Model):
auto_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
place = models.ForeignKey(Place)
def __unicode__(self):
return "%s at %s" % (self.name, self.place)
class Location(models.Model):
place = models.ForeignKey(Place, unique=True)
# this is purely for testing the data doesn't matter here :)
lat = models.CharField(max_length=100)
lon = models.CharField(max_length=100)
class OwnerProfile(models.Model):
owner = models.OneToOneField(Owner, primary_key=True)
age = models.PositiveIntegerField()
def __unicode__(self):
return "%s is %d" % (self.owner.name, self.age)
class Restaurant(Place):
serves_pizza = models.BooleanField()
def __unicode__(self):
return self.name
class Product(models.Model):
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.slug
class Price(models.Model):
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField()
def __unicode__(self):
return u"%s for %s" % (self.quantity, self.price)
class Meta:
unique_together = (('price', 'quantity'),)
class MexicanRestaurant(Restaurant):
serves_tacos = models.BooleanField()
class ClassyMexicanRestaurant(MexicanRestaurant):
restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
tacos_are_yummy = models.BooleanField()
# models for testing unique_together validation when a fk is involved and
# using inlineformset_factory.
class Repository(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Revision(models.Model):
repository = models.ForeignKey(Repository)
revision = models.CharField(max_length=40)
class Meta:
unique_together = (("repository", "revision"),)
def __unicode__(self):
return u"%s (%s)" % (self.revision, unicode(self.repository))
# models for testing callable defaults (see bug #7975). If you define a model
# with a callable default value, you cannot rely on the initial value in a
# form.
class Person(models.Model):
name = models.CharField(max_length=128)
class Membership(models.Model):
person = models.ForeignKey(Person)
date_joined = models.DateTimeField(default=datetime.datetime.now)
karma = models.IntegerField()
# models for testing a null=True fk to a parent
class Team(models.Model):
name = models.CharField(max_length=100)
class Player(models.Model):
team = models.ForeignKey(Team, null=True)
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
# Models for testing custom ModelForm save methods in formsets and inline formsets
class Poet(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Poem(models.Model):
poet = models.ForeignKey(Poet)
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
posted = models.DateField()
def __unicode__(self):
return self.name
__test__ = {'API_TESTS': """
>>> from datetime import date
>>> from django.forms.models import modelformset_factory
>>> qs = Author.objects.all()
>>> AuthorFormSet = modelformset_factory(Author, extra=3)
>>> formset = AuthorFormSet(queryset=qs)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'form-TOTAL_FORMS': '3', # the number of forms rendered
... 'form-INITIAL_FORMS': '0', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-name': 'Charles Baudelaire',
... 'form-1-name': 'Arthur Rimbaud',
... 'form-2-name': '',
... }
>>> formset = AuthorFormSet(data=data, queryset=qs)
>>> formset.is_valid()
True
>>> formset.save()
[, ]
>>> for author in Author.objects.order_by('name'):
... print author.name
Arthur Rimbaud
Charles Baudelaire
Gah! We forgot Paul Verlaine. Let's create a formset to edit the existing
authors with an extra form to add him. We *could* pass in a queryset to
restrict the Author objects we edit, but in this case we'll use it to display
them in alphabetical order by name.
>>> qs = Author.objects.order_by('name')
>>> AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=False)
>>> formset = AuthorFormSet(queryset=qs)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'form-TOTAL_FORMS': '3', # the number of forms rendered
... 'form-INITIAL_FORMS': '2', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-id': '2',
... 'form-0-name': 'Arthur Rimbaud',
... 'form-1-id': '1',
... 'form-1-name': 'Charles Baudelaire',
... 'form-2-name': 'Paul Verlaine',
... }
>>> formset = AuthorFormSet(data=data, queryset=qs)
>>> formset.is_valid()
True
# Only changed or new objects are returned from formset.save()
>>> formset.save()
[]
>>> for author in Author.objects.order_by('name'):
... print author.name
Arthur Rimbaud
Charles Baudelaire
Paul Verlaine
This probably shouldn't happen, but it will. If an add form was marked for
deltetion, make sure we don't save that form.
>>> qs = Author.objects.order_by('name')
>>> AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=True)
>>> formset = AuthorFormSet(queryset=qs)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'form-TOTAL_FORMS': '4', # the number of forms rendered
... 'form-INITIAL_FORMS': '3', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-id': '2',
... 'form-0-name': 'Arthur Rimbaud',
... 'form-1-id': '1',
... 'form-1-name': 'Charles Baudelaire',
... 'form-2-id': '3',
... 'form-2-name': 'Paul Verlaine',
... 'form-3-name': 'Walt Whitman',
... 'form-3-DELETE': 'on',
... }
>>> formset = AuthorFormSet(data=data, queryset=qs)
>>> formset.is_valid()
True
# No objects were changed or saved so nothing will come back.
>>> formset.save()
[]
>>> for author in Author.objects.order_by('name'):
... print author.name
Arthur Rimbaud
Charles Baudelaire
Paul Verlaine
Let's edit a record to ensure save only returns that one record.
>>> data = {
... 'form-TOTAL_FORMS': '4', # the number of forms rendered
... 'form-INITIAL_FORMS': '3', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-id': '2',
... 'form-0-name': 'Walt Whitman',
... 'form-1-id': '1',
... 'form-1-name': 'Charles Baudelaire',
... 'form-2-id': '3',
... 'form-2-name': 'Paul Verlaine',
... 'form-3-name': '',
... 'form-3-DELETE': '',
... }
>>> formset = AuthorFormSet(data=data, queryset=qs)
>>> formset.is_valid()
True
# One record has changed.
>>> formset.save()
[]
Test the behavior of commit=False and save_m2m
>>> meeting = AuthorMeeting.objects.create(created=date.today())
>>> meeting.authors = Author.objects.all()
# create an Author instance to add to the meeting.
>>> new_author = Author.objects.create(name=u'John Steinbeck')
>>> AuthorMeetingFormSet = modelformset_factory(AuthorMeeting, extra=1, can_delete=True)
>>> data = {
... 'form-TOTAL_FORMS': '2', # the number of forms rendered
... 'form-INITIAL_FORMS': '1', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-id': '1',
... 'form-0-name': '2nd Tuesday of the Week Meeting',
... 'form-0-authors': [2, 1, 3, 4],
... 'form-1-name': '',
... 'form-1-authors': '',
... 'form-1-DELETE': '',
... }
>>> formset = AuthorMeetingFormSet(data=data, queryset=AuthorMeeting.objects.all())
>>> formset.is_valid()
True
>>> instances = formset.save(commit=False)
>>> for instance in instances:
... instance.created = date.today()
... instance.save()
>>> formset.save_m2m()
>>> instances[0].authors.all()
[, , , ]
# delete the author we created to allow later tests to continue working.
>>> new_author.delete()
Test the behavior of max_num with model formsets. It should allow all existing
related objects/inlines for a given object to be displayed, but not allow
the creation of new inlines beyond max_num.
>>> qs = Author.objects.order_by('name')
>>> AuthorFormSet = modelformset_factory(Author, max_num=None, extra=3)
>>> formset = AuthorFormSet(queryset=qs)
>>> len(formset.forms)
6
>>> len(formset.extra_forms)
3
>>> AuthorFormSet = modelformset_factory(Author, max_num=4, extra=3)
>>> formset = AuthorFormSet(queryset=qs)
>>> len(formset.forms)
4
>>> len(formset.extra_forms)
1
>>> AuthorFormSet = modelformset_factory(Author, max_num=0, extra=3)
>>> formset = AuthorFormSet(queryset=qs)
>>> len(formset.forms)
3
>>> len(formset.extra_forms)
0
>>> AuthorFormSet = modelformset_factory(Author, max_num=None)
>>> formset = AuthorFormSet(queryset=qs)
>>> [x.name for x in formset.get_queryset()]
[u'Charles Baudelaire', u'Paul Verlaine', u'Walt Whitman']
>>> AuthorFormSet = modelformset_factory(Author, max_num=0)
>>> formset = AuthorFormSet(queryset=qs)
>>> [x.name for x in formset.get_queryset()]
[u'Charles Baudelaire', u'Paul Verlaine', u'Walt Whitman']
>>> AuthorFormSet = modelformset_factory(Author, max_num=4)
>>> formset = AuthorFormSet(queryset=qs)
>>> [x.name for x in formset.get_queryset()]
[u'Charles Baudelaire', u'Paul Verlaine', u'Walt Whitman']
# ModelForm with a custom save method in a formset ###########################
>>> class PoetForm(forms.ModelForm):
... def save(self, commit=True):
... # change the name to "Vladimir Mayakovsky" just to be a jerk.
... author = super(PoetForm, self).save(commit=False)
... author.name = u"Vladimir Mayakovsky"
... if commit:
... author.save()
... return author
>>> PoetFormSet = modelformset_factory(Poet, form=PoetForm)
>>> data = {
... 'form-TOTAL_FORMS': '3', # the number of forms rendered
... 'form-INITIAL_FORMS': '0', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-name': 'Walt Whitman',
... 'form-1-name': 'Charles Baudelaire',
... 'form-2-name': '',
... }
>>> qs = Poet.objects.all()
>>> formset = PoetFormSet(data=data, queryset=qs)
>>> formset.is_valid()
True
>>> formset.save()
[, ]
# Model inheritance in model formsets ########################################
>>> BetterAuthorFormSet = modelformset_factory(BetterAuthor)
>>> formset = BetterAuthorFormSet()
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'form-TOTAL_FORMS': '1', # the number of forms rendered
... 'form-INITIAL_FORMS': '0', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-author_ptr': '',
... 'form-0-name': 'Ernest Hemingway',
... 'form-0-write_speed': '10',
... }
>>> formset = BetterAuthorFormSet(data)
>>> formset.is_valid()
True
>>> formset.save()
[]
>>> hemingway_id = BetterAuthor.objects.get(name="Ernest Hemingway").pk
>>> formset = BetterAuthorFormSet()
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'form-TOTAL_FORMS': '2', # the number of forms rendered
... 'form-INITIAL_FORMS': '1', # the number of forms with initial data
... 'form-MAX_NUM_FORMS': '', # the max number of forms
... 'form-0-author_ptr': hemingway_id,
... 'form-0-name': 'Ernest Hemingway',
... 'form-0-write_speed': '10',
... 'form-1-author_ptr': '',
... 'form-1-name': '',
... 'form-1-write_speed': '',
... }
>>> formset = BetterAuthorFormSet(data)
>>> formset.is_valid()
True
>>> formset.save()
[]
# Inline Formsets ############################################################
We can also create a formset that is tied to a parent model. This is how the
admin system's edit inline functionality works.
>>> from django.forms.models import inlineformset_factory
>>> AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=3)
>>> author = Author.objects.get(name='Charles Baudelaire')
>>> formset = AuthorBooksFormSet(instance=author)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'book_set-MAX_NUM_FORMS': '', # the max number of forms
... 'book_set-0-title': 'Les Fleurs du Mal',
... 'book_set-1-title': '',
... 'book_set-2-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[]
>>> for book in author.book_set.all():
... print book.title
Les Fleurs du Mal
Now that we've added a book to Charles Baudelaire, let's try adding another
one. This time though, an edit form will be available for every existing
book.
>>> AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2)
>>> author = Author.objects.get(name='Charles Baudelaire')
>>> formset = AuthorBooksFormSet(instance=author)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
... 'book_set-MAX_NUM_FORMS': '', # the max number of forms
... 'book_set-0-id': '1',
... 'book_set-0-title': 'Les Fleurs du Mal',
... 'book_set-1-title': 'Les Paradis Artificiels',
... 'book_set-2-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[]
As you can see, 'Les Paradis Artificiels' is now a book belonging to Charles Baudelaire.
>>> for book in author.book_set.order_by('id'):
... print book.title
Les Fleurs du Mal
Les Paradis Artificiels
The save_as_new parameter lets you re-associate the data to a new instance.
This is used in the admin for save_as functionality.
>>> data = {
... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '2', # the number of forms with initial data
... 'book_set-MAX_NUM_FORMS': '', # the max number of forms
... 'book_set-0-id': '1',
... 'book_set-0-title': 'Les Fleurs du Mal',
... 'book_set-1-id': '2',
... 'book_set-1-title': 'Les Paradis Artificiels',
... 'book_set-2-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=Author(), save_as_new=True)
>>> formset.is_valid()
True
>>> new_author = Author.objects.create(name='Charles Baudelaire')
>>> formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True)
>>> [book for book in formset.save() if book.author.pk == new_author.pk]
[, ]
Test using a custom prefix on an inline formset.
>>> formset = AuthorBooksFormSet(prefix="test")
>>> for form in formset.forms:
... print form.as_p()
Test inline formsets where the inline-edited object has a custom primary key that is not the fk to the parent object.
>>> AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1)
>>> formset = AuthorBooksFormSet2(instance=author)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'bookwithcustompk_set-TOTAL_FORMS': '1', # the number of forms rendered
... 'bookwithcustompk_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'bookwithcustompk_set-MAX_NUM_FORMS': '', # the max number of forms
... 'bookwithcustompk_set-0-my_pk': '77777',
... 'bookwithcustompk_set-0-title': 'Les Fleurs du Mal',
... }
>>> formset = AuthorBooksFormSet2(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[]
>>> for book in author.bookwithcustompk_set.all():
... print book.title
Les Fleurs du Mal
Test inline formsets where the inline-edited object uses multi-table inheritance, thus
has a non AutoField yet auto-created primary key.
>>> AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1)
>>> formset = AuthorBooksFormSet3(instance=author)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'alternatebook_set-TOTAL_FORMS': '1', # the number of forms rendered
... 'alternatebook_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'alternatebook_set-MAX_NUM_FORMS': '', # the max number of forms
... 'alternatebook_set-0-title': 'Flowers of Evil',
... 'alternatebook_set-0-notes': 'English translation of Les Fleurs du Mal'
... }
>>> formset = AuthorBooksFormSet3(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[]
Test inline formsets where the inline-edited object has a unique_together constraint with a nullable member
>>> AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2)
>>> data = {
... 'bookwithoptionalalteditor_set-TOTAL_FORMS': '2', # the number of forms rendered
... 'bookwithoptionalalteditor_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'bookwithoptionalalteditor_set-MAX_NUM_FORMS': '', # the max number of forms
... 'bookwithoptionalalteditor_set-0-author': '1',
... 'bookwithoptionalalteditor_set-0-title': 'Les Fleurs du Mal',
... 'bookwithoptionalalteditor_set-1-author': '1',
... 'bookwithoptionalalteditor_set-1-title': 'Les Fleurs du Mal',
... }
>>> formset = AuthorBooksFormSet4(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[, ]
# ModelForm with a custom save method in an inline formset ###################
>>> class PoemForm(forms.ModelForm):
... def save(self, commit=True):
... # change the name to "Brooklyn Bridge" just to be a jerk.
... poem = super(PoemForm, self).save(commit=False)
... poem.name = u"Brooklyn Bridge"
... if commit:
... poem.save()
... return poem
>>> PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemForm)
>>> data = {
... 'poem_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'poem_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'poem_set-MAX_NUM_FORMS': '', # the max number of forms
... 'poem_set-0-name': 'The Cloud in Trousers',
... 'poem_set-1-name': 'I',
... 'poem_set-2-name': '',
... }
>>> poet = Poet.objects.create(name='Vladimir Mayakovsky')
>>> formset = PoemFormSet(data=data, instance=poet)
>>> formset.is_valid()
True
>>> formset.save()
[, ]
We can provide a custom queryset to our InlineFormSet:
>>> custom_qs = Book.objects.order_by('-title')
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'book_set-TOTAL_FORMS': '5', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '3', # the number of forms with initial data
... 'book_set-MAX_NUM_FORMS': '', # the max number of forms
... 'book_set-0-id': '1',
... 'book_set-0-title': 'Les Fleurs du Mal',
... 'book_set-1-id': '2',
... 'book_set-1-title': 'Les Paradis Artificiels',
... 'book_set-2-id': '5',
... 'book_set-2-title': 'Flowers of Evil',
... 'book_set-3-title': 'Revue des deux mondes',
... 'book_set-4-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
>>> formset.is_valid()
True
>>> custom_qs = Book.objects.filter(title__startswith='F')
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
>>> for form in formset.forms:
... print form.as_p()
>>> data = {
... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
... 'book_set-MAX_NUM_FORMS': '', # the max number of forms
... 'book_set-0-id': '5',
... 'book_set-0-title': 'Flowers of Evil',
... 'book_set-1-title': 'Revue des deux mondes',
... 'book_set-2-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
>>> formset.is_valid()
True
# Test a custom primary key ###################################################
We need to ensure that it is displayed
>>> CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey)
>>> formset = CustomPrimaryKeyFormSet()
>>> for form in formset.forms:
... print form.as_p()
# Custom primary keys with ForeignKey, OneToOneField and AutoField ############
>>> place = Place(name=u'Giordanos', city=u'Chicago')
>>> place.save()
>>> FormSet = inlineformset_factory(Place, Owner, extra=2, can_delete=False)
>>> formset = FormSet(instance=place)
>>> for form in formset.forms:
... print form.as_p()