ModelForm usage

107 views
Skip to first unread message

gerhar...@ogersoft.at

unread,
Nov 11, 2009, 5:04:29 AM11/11/09
to django...@googlegroups.com
Hello

I am new to django and try to use the ModelForm paradigma, but can't get the
point ;-((


My example:

models.py:
class Project(models.Model):
id = models.AutoField(primary_key=True)
deletetime = models.DateTimeField()
name = models.CharField(max_length=50)
beginDate = models.DateField()
endDate = models.DateField()
memo = models.TextField()

Doing:
project = Project()
project.save()
works fine (autofield increased, other fields empty)


forms.py:
class ProjectForm(ModelForm):
class Meta:
model = models.Project


- first try for empty record, expecting an empty Project-object to be created
from the form automatically:
projectForm = forms.ProjectForm()
projectForm.save()
=> result: 'ProjectForm' object has no attribute 'cleaned_data'

- second try for empty record by providing an empty Project-instance:
project = Project()
projectForm = forms.ProjectForm(instance=project)
projectForm.save()
=> result: 'ProjectForm' object has no attribute 'cleaned_data'

The _errors and errors attribute are present, but empty: {}


- first try to save an existing unmodified record:
project = Project.objects.get(id=1)
projectForm = forms.ProjectForm(instance=project)
project = projectForm.save()
=> result: 'ProjectForm' object has no attribute 'cleaned_data'

- second try to save an existing record with data from request.POST:
project = Project.objects.get(id=1)
projectForm = forms.ProjectForm(request.POST, instance=project)
project = projectForm.save()
The content of request.POST:
<QueryDict: {u'endDate': [u''], u'name': [u'Maissau'], u'memo': [u'aaa'], u'id':
[u'1'], u'savebutton': [u'savebutton'], u'beginDate': [u'2009-10-11']}>
=> result: The Project could not be changed because the data didn't validate.

Can anybody point me the right direction?


Thanks in advance
gerhard


--
O(ettl) GER(hard)

Daniel Roseman

unread,
Nov 11, 2009, 8:28:04 AM11/11/09
to Django users
On Nov 11, 10:04 am, gerhard.oe...@ogersoft.at wrote:
> Hello
>
> I am new to django and try to use the ModelForm paradigma, but can't get the
> point ;-((

As the documentation says, the point of a modelform is firstly to
validate input from a user and save it to the database, and secondly
to display an HTML form for the user to enter/edit that data.

>
> My example:
>
> models.py:
> class Project(models.Model):
>      id = models.AutoField(primary_key=True)
>      deletetime = models.DateTimeField()
>      name = models.CharField(max_length=50)
>      beginDate = models.DateField()
>      endDate = models.DateField()
>      memo = models.TextField()
>
> Doing:
> project = Project()
> project.save()
> works fine (autofield increased, other fields empty)
>
> forms.py:
> class ProjectForm(ModelForm):
>      class Meta:
>          model = models.Project
>
<snip>

> - second try to save an existing record with data from request.POST:
> project = Project.objects.get(id=1)
> projectForm = forms.ProjectForm(request.POST, instance=project)
> project = projectForm.save()
> The content of request.POST:
> <QueryDict: {u'endDate': [u''], u'name': [u'Maissau'], u'memo': [u'aaa'], u'id':
> [u'1'], u'savebutton': [u'savebutton'], u'beginDate': [u'2009-10-11']}>
> => result: The Project could not be changed because the data didn't validate.

As this states, the data didn't validate: your model doesn't have
blank=True on any of its attributes, so they are all required, but you
don't provide endDate. If you did projectForm.is_valid() and then
looked at projectForm.errors it would probably tell you this.
--
DR.

pjrh...@gmail.com

unread,
Nov 12, 2009, 1:40:17 PM11/12/09
to Django users
> - first try for empty record, expecting an empty Project-object to be created
> from the form automatically:
> projectForm = forms.ProjectForm()
> projectForm.save()
> => result: 'ProjectForm' object has no attribute 'cleaned_data'
You need to run the clean method on the form to make sure the data is
valid, for example:

projectForm = forms.ProjectForm()
if projectForm.is_valid(): #this runs clean()
projectForm.save()
else:
print projectForm.errors

I would expect this to just print lots of errors at you because your
model doesn't allow those fields to be blank. I'm slightly suspect
that some of your example here isn't accurate though, as you say you
can save a blank model which given your model definition would raise
an error.

Peter

gerhar...@ogersoft.at

unread,
Nov 20, 2009, 4:39:50 AM11/20/09
to django...@googlegroups.com
Daniel Roseman schrieb:
> On Nov 11, 10:04 am, gerhard.oe...@ogersoft.at wrote:
>> Hello
>>
>> I am new to django and try to use the ModelForm paradigma, but can't get the
>> point ;-((
>
> As the documentation says, the point of a modelform is firstly to
> validate input from a user and save it to the database, and secondly
> to display an HTML form for the user to enter/edit that data.
>
>> My example (extended with blank=True attributes):
>>
>> models.py:

class Project(models.Model):
id = models.AutoField(primary_key=True, blank=True)
deletetime = models.DateTimeField(blank=True, null=True)
name = models.CharField(max_length=50)
beginDate = models.DateField()
endDate = models.DateField(blank=True, null=True)
memo = models.TextField(blank=True, null=True)

>> Doing:
>> project = Project()
>> project.save()
>> works fine (autofield increased, other fields empty)
>>
>> forms.py:
>> class ProjectForm(ModelForm):
>> class Meta:
>> model = models.Project
>>
> <snip>
>
>> - second try to save an existing record with data from request.POST:
>> project = Project.objects.get(id=1)
>> projectForm = forms.ProjectForm(request.POST, instance=project)
>> project = projectForm.save()
>> The content of request.POST:
>> <QueryDict: {u'endDate': [u''], u'name': [u'Maissau'], u'memo': [u'aaa'], u'id':
>> [u'1'], u'savebutton': [u'savebutton'], u'beginDate': [u'2009-10-11']}>
>> => result: The Project could not be changed because the data didn't validate.
>
> As this states, the data didn't validate: your model doesn't have
> blank=True on any of its attributes, so they are all required, but you
> don't provide endDate.

Model modified.

Error message changes to: 'ProjectForm' object has no attribute 'cleaned_data'

So whatever I tried gives the same error message.

> If you did projectForm.is_valid() and then
> looked at projectForm.errors it would probably tell you this.

I did this before posting the first time with the same results as now:
projectForm.is_valid() returns False
projectForm.errors is empty
(output of str(projectForm.errors) and when using winpdb)

So the question remains: How can I detect what does not validate?



gerhard
Reply all
Reply to author
Forward
0 new messages