Hi,
I have a form where I have added on clean method for a field and a
general clean method for the form. In generall I see a problem, when
trying to save the form. Here is a very simple test case, where I have
commented out the "clean" method of the form:
>>> from scrums.forms import SprintForm
>>> import datetime
>>> form = SprintForm(data = {'name': 'foo','description': 'bar','start_date':
>>> datetime.date(2108,12,23), 'end_date': datetime.date(2109,01,11)})
>>> print form.is_valid()
True
>>> new_sprint = form.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.6/site-packages/django/forms/models.py",
line 313, in save
fail_message, commit, construct=False)
File "/Library/Python/2.6/site-packages/django/forms/models.py",
line 95, in save_instance
instance.save()
File "/Library/Python/2.6/site-packages/django/db/models/base.py",
line 430, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/Library/Python/2.6/site-packages/django/db/models/base.py",
line 519, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/Library/Python/2.6/site-packages/django/db/models/
manager.py", line 197, in _insert
return insert_query(self.model, values, **kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/query.py",
line 1345, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Python/2.6/site-packages/django/db/models/sql/
compiler.py", line 730, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/Library/Python/2.6/site-packages/django/db/models/sql/
compiler.py", line 674, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/util.py",
line 19, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/
base.py", line 189, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: scrums_sprint.creator_id may not be NULL
>>> new_sprint = form.save(commit = False)
>>> print new_sprint
foo <############## notice this
>>>
When I add back the clean method, I see this:
>>> from scrums.forms import SprintForm
>>> import datetime
>>> form = SprintForm(data = {'name': 'foo','description': 'bar','start_date':
>>> datetime.date(2108,12,23), 'end_date': datetime.date(2109,01,11)})
>>> print form.is_valid()
True
>>> new_sprint = form.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.6/site-packages/django/forms/models.py",
line 313, in save
fail_message, commit, construct=False)
File "/Library/Python/2.6/site-packages/django/forms/models.py",
line 95, in save_instance
instance.save()
File "/Library/Python/2.6/site-packages/django/db/models/base.py",
line 430, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/Library/Python/2.6/site-packages/django/db/models/base.py",
line 519, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/Library/Python/2.6/site-packages/django/db/models/
manager.py", line 197, in _insert
return insert_query(self.model, values, **kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/query.py",
line 1345, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Python/2.6/site-packages/django/db/models/sql/
compiler.py", line 730, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/Library/Python/2.6/site-packages/django/db/models/sql/
compiler.py", line 674, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/util.py",
line 19, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/
base.py", line 189, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: scrums_sprint.start_date may not be NULL
>>> new_sprint = form.save(commit = False)
>>> print new_sprint
<################### no name, no dates, nothing
>>>
and this is the clean method:
def clean(self):
cleaned_data = self.cleaned_data
start_date = cleaned_data.get('start_date')
end_date = cleaned_data.get('end_date')
if start_date and end_date:
if start_date > end_date:
raise forms.ValidationError(u'Start date must be
before end date.')
return cleaned_data
There must be an error I have introduced, but I can't see it.
I use this version of django:
Path: .
URL: http://code.djangoproject.com/svn/django/trunk
Repository Root: http://code.djangoproject.com/svn
Repository UUID: bcc190cf-cafb-0310-a4f2-bffc1f526a37
Revision: 12231
Node Kind: directory
Schedule: normal
Last Changed Author: lukeplant
Last Changed Rev: 12229
Last Changed Date: 2010-01-16 04:13:16 +0100 (Sa, 16 Jan 2010)
many thanks, tom
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.