from django.db import models try: sorted except NameError: from django.utils.itercompat import sorted 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) def __unicode__(self): return self.title 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): name = models.CharField(max_length=100) place = models.ForeignKey(Place) class Restaurant(Place): serves_pizza = models.BooleanField() def __unicode__(self): return self.name class MexicanRestaurant(Restaurant): serves_tacos = models.BooleanField() __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-0-name': 'Charles Baudelaire', ... 'form-1-name': 'Arthur Rimbaud', ... 'form-2-name': '', ... } >>> formset = AuthorFormSet(data=data, queryset=qs) >>> formset.is_valid() True >>> formset.save() [
>>> data = { ... 'form-TOTAL_FORMS': '3', # the number of forms rendered ... 'form-INITIAL_FORMS': '2', # the number of forms with initial data ... '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() [
>>> data = { ... 'form-TOTAL_FORMS': '4', # the number of forms rendered ... 'form-INITIAL_FORMS': '3', # the number of forms with initial data ... '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-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() [
>>> data = { ... 'form-TOTAL_FORMS': '1', # the number of forms rendered ... 'form-INITIAL_FORMS': '0', # the number of forms with initial data ... 'form-0-author_ptr_id': '', ... 'form-0-name': 'Ernest Hemingway', ... 'form-0-write_speed': '10', ... } >>> formset = BetterAuthorFormSet(data) >>> formset.is_valid() True >>> formset.save() [
>>> data = { ... 'form-TOTAL_FORMS': '2', # the number of forms rendered ... 'form-INITIAL_FORMS': '1', # the number of forms with initial data ... 'form-0-author_ptr_id': hemingway_id, ... 'form-0-name': 'Ernest Hemingway', ... 'form-0-write_speed': '10', ... 'form-1-author_ptr_id': '', ... '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-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() [
>>> 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-0-id': '1', ... 'book_set-0-title': 'Les Fleurs du Mal', ... 'book_set-1-title': 'Le Spleen de Paris', ... 'book_set-2-title': '', ... } >>> formset = AuthorBooksFormSet(data, instance=author) >>> formset.is_valid() True >>> formset.save() [
# 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()
# Foreign keys in parents ######################################## >>> from django.forms.models import _get_foreign_key >>> type(_get_foreign_key(Restaurant, Owner))