Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Django InlineFormset ValueError

View models.py
1 2 3 4 5 6 7 8
from django.db import models
 
class Container(models.Model):
pass
 
class Item(models.Model):
container = models.ForeignKey(Container)
data = models.CharField(max_length=123)
View models.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 
from django.test import TestCase
from django.forms.models import inlineformset_factory, BaseInlineFormSet
from test_app.models import Container, Item
 
TestFormSet = inlineformset_factory(Container, Item, extra=0)
 
class MyTestCase(TestCase):
def setUp(self):
self.container = Container.objects.create()
self.existingItem = Item.objects.create(container=self.container, data="asdf")
self.test_data = {
"item_set-TOTAL_FORMS": "2",
"item_set-MAX_NUM_FORMS": "1000",
"item_set-INITIAL_FORMS": "1",
"item_set-0-id": "", # Not valid
"item_set-0-data": "foo",
"item_set-1-id": str(self.existingItem.id),
"item_set-1-data": str(self.existingItem.data),
}
def test_django_formset_int_error(self):
formset = TestFormSet(self.test_data, instance=self.container)
try:
formset.is_valid()
except ValueError:
self.fail("Formset.is_valid() raised unexpected exception.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.