ModelForms and ForeignKeys causing ValueError

40 views
Skip to first unread message

Peter Bengtsson

unread,
Jun 14, 2010, 12:39:24 PM6/14/10
to Django developers
I'm happy to submit a ticket but wanted to check first that I'm doing
the right thing. I think this used to work in Django 1.1 but not now
in trunk.

I can better explain it with code:

# models.py
class FooBarModel(models.Model):
name = models.CharField(max_length=10)
age = models.IntegerField()
user = models.ForeignKey(User)

# forms.py
class FooBarForm(forms.ModelForm):
class Meta:
model = FooBarModel

def __init__(self, *a, **k):
super(FooBarForm, self).__init__(*a, **k)
for field in self.fields:
self.fields[field].required = False

The point of this is that I can make an advanced search form where the
form is just like the form for added one of these models. In my view I
never call the save() method of the form instance.

# tests.py
class SimpleTest(TestCase):
def test_model_forms(self):
from forms import FooBarForm
post = {'name':'Peter'}
f2 = FooBarForm(data=post)
self.assertTrue(not f2.is_valid())


Instead of returning False on the is_valid() function, it raises a
ValueError which looks like this:

Traceback (most recent call last):
File "/home/peterbe/dev/DJANGO/lc/debugapp/tests.py", line 14, in
test_model_forms
print f2.is_valid()
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/forms/forms.py", line 121, in is_valid
return self.is_bound and not bool(self.errors)
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/forms/forms.py", line 112, in
_get_errors
self.full_clean()
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/forms/forms.py", line 269, in full_clean
self._post_clean()
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/forms/models.py", line 317, in
_post_clean
self.instance = construct_instance(self, self.instance,
opts.fields, opts.exclude)
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/forms/models.py", line 51, in
construct_instance
f.save_form_data(instance, cleaned_data[f.name])
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/db/models/fields/__init__.py", line 416,
in save_form_data
setattr(instance, self.name, data)
File "/home/peterbe/virtualenvs/lc/lib/python2.5/site-packages/
Django-1.2.1-py2.5.egg/django/db/models/fields/related.py", line 314,
in __set__
(instance._meta.object_name, self.field.name))
ValueError: Cannot assign None: "FooBarModel.user" does not allow null
values.


I can understand that the field doesn't like having None set as a
value because it will never allow the save but I'm not interested in
saving here. I'm interested in getting a form that models (no pun
intended) itself on a model.


So, bug or feature?

Karen Tracey

unread,
Jun 14, 2010, 2:05:01 PM6/14/10
to django-d...@googlegroups.com
On Mon, Jun 14, 2010 at 12:39 PM, Peter Bengtsson <pet...@gmail.com> wrote:
I'm happy to submit a ticket but wanted to check first that I'm doing
the right thing. I think this used to work in Django 1.1 but not now
in trunk.

I can better explain it with code:

[snip details]

The point of this is that I can make an advanced search form where the
form is just like the form for added one of these models. In my view I
never call the save() method of the form instance.

# tests.py
class SimpleTest(TestCase):
   def test_model_forms(self):
       from forms import FooBarForm
       post = {'name':'Peter'}
       f2 = FooBarForm(data=post)
       self.assertTrue(not f2.is_valid())


Instead of returning False on the is_valid() function, it raises a
ValueError which looks like this:

[snip details]


I can understand that the field doesn't like having None set as a
value because it will never allow the save but I'm not interested in
saving here. I'm interested in getting a form that models (no pun
intended) itself on a model.


So, bug or feature?

Looks like a bug to me. is_valid() ought not be raising a ValueError...worst case it should return False. But in this case True would seem to be the correct answer, since you've set the field to not required.

Karen

Peter Bengtsson

unread,
Jun 16, 2010, 9:42:12 AM6/16/10
to Django developers
Submitted here: http://code.djangoproject.com/ticket/13776

On Jun 14, 2:05 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages