I have a very simple db.Model
that contains a db.IntegerProperty
and a db.TextProperty
:
class Foo(db.Model):
shoe_size = db.IntegerProperty()
comments = db.TextProperty()
From this I created a very simple Django Form Object:
class FooForm(djangoforms.ModelForm):
shoe_size = forms.IntegerField(required=False)
comments = forms.CharField(required=False,
widget=forms.Textarea(attrs={'rows':5, 'cols':30}))
class Meta:
model = models.Foo
I created a standard Django Form using the FormObject and a handler to handle POSTs, all very simple stuff running on Google App Engine. However, I just noticed that I cannot UPDATE an existing shoe_size value back to NULL once it is set. I.e. if I submit a form with a shoe_size with a blank value, no problem. If I submit a form with a shoe_size with a proper value (e.g. 13), no problem. HOWEVER, if I try to update that value back to NULL, i.e. blank the form field, nothing happens. The form gets submitted successfully, but 13 stays in the DB, it never gets updated to NULL. The exact same scenario works perfect for the comments field, I can set it back to NULL/Blank, but it does not work for any db.IntegerProperty().
My Handler looks like this:
def foo_form (request, foo_key=None):
if request.method == 'POST':
# The form was submitted.
if foo_key:
foo = db.get(db.Key(encoded=foo_key))
form = FooForm(request.POST, instance=foo)
else:
# Create a new Foo based on the form.
foo = FooForm(request.POST)
if form.is_valid():
foo = form.save(commit=False)
foo.put()
return HttpResponseRedirect('/foo/')
else:
# The user wants to see the form.
if foo_key:
# Show the form to edit an existing Foo.
foo = db.get(db.Key(encoded=foo_key))
form = FooForm(instance=foo)
else:
# Show the form to create a new Foo.
form = FooForm()
template_values['form'] = form
return render_to_response('foo/foo_form.html',
template_values
)
I added some logging and can clearly see that a blank value comes back from the form, however, when I do a form.save(), that blank value is ignored for the integer. Interestingly, the comments String shows "None" in the log when I blank it out whereas the Integer shows nothing:
INFO 2010-09-24 19:15:28,358 views.py:13] Entering foo_form
INFO 2010-09-24 19:15:28,358 views.py:22] !!!!!!
INFO 2010-09-24 19:15:28,358 views.py:24] foo from DB
INFO 2010-09-24 19:15:28,358 views.py:25] 74
INFO 2010-09-24 19:15:28,358 views.py:26] Comment
INFO 2010-09-24 19:15:28,358 views.py:27] foo from POST
INFO 2010-09-24 19:15:28,358 views.py:28]
INFO 2010-09-24 19:15:28,358 views.py:29]
INFO 2010-09-24 19:15:28,375 views.py:35] cleared_data
INFO 2010-09-24 19:15:28,375 views.py:36] foo from Form
INFO 2010-09-24 19:15:28,375 views.py:37]
INFO 2010-09-24 19:15:28,375 views.py:38] None
INFO 2010-09-24 19:15:28,375 views.py:40] !!!!!!
INFO 2010-09-24 19:15:28,375 views.py:41] foo from after Save
INFO 2010-09-24 19:15:28,375 views.py:42] 74
INFO 2010-09-24 19:15:28,375 views.py:43] None
INFO 2010-09-24 19:15:28,375 views.py:44] !!!!!!
I tried adding default=None
to the db.Model
for the Integer field but that did not solve my problem.