Inserting Into Cleaned_Data Inside a ModelForm

1,399 views
Skip to first unread message

Chris

unread,
Mar 6, 2009, 8:13:48 AM3/6/09
to Django users
I have a model that has a required attribute, but I want this
attribute set to a calculated value based on the input from a form. So
inside the save() method of my ModelForm class, I'm doing:

self.cleaned_data['requiredKey'] = 123
record = forms.ModelForm.save(self)

However, this raises the error "ValueError: The Record could not be
created (Property requiredKey is required)".

Is cleaned_data read-only? How would I modify the data used when
creating the model? I realize I could probably set requiredKey as a
form field, and then set it in clean_requiredKey(), but I don't want
the field included in the actual form (not even as a hidden field).

Any help is appreciated.

Chris

Waldemar Kornewald

unread,
Mar 6, 2009, 10:43:12 AM3/6/09
to Django users
On 6 Mrz., 14:13, Chris <chriss...@gmail.com> wrote:
> I have a model that has a required attribute, but I want this
> attribute set to a calculated value based on the input from a form. So
> inside the save() method of my ModelForm class, I'm doing:
>
> self.cleaned_data['requiredKey'] = 123
> record = forms.ModelForm.save(self)

Just for reference:
http://groups.google.com/group/app-engine-patch/msg/a9ed56aaf326b17e

Bye,
Waldemar Kornewald

rajeesh

unread,
Mar 6, 2009, 12:57:34 PM3/6/09
to Django users
You may change the save_model() method of corresponding AdminClass for
that:
e.g, to set the attribute 'a' of model 'Book' , write inside
BookAdmin.save_model() something like this:

obj.a = form.cleaned_data['another_key'] * 2
obj.save()

Chris Spencer

unread,
Mar 7, 2009, 12:13:54 PM3/7/09
to django...@googlegroups.com
On Fri, Mar 6, 2009 at 12:57 PM, rajeesh <rajees...@gmail.com> wrote:
>
> You may change the save_model() method of corresponding AdminClass for
> that:
> e.g, to set the attribute 'a' of model 'Book' , write inside
> BookAdmin.save_model() something like this:
>
> obj.a = form.cleaned_data['another_key'] * 2
> obj.save()

Won't this only effect admin? I'm looking for a general solution that
I can work into any form.

Chris

Briel

unread,
Mar 7, 2009, 6:15:31 PM3/7/09
to Django users
When using a modelform there are some ways to get you where you want.
You can get your form using request.POST. If it validates you can save
it like this creating a model.

record = form.save(commit=false)
record.key = 123
record.save()

Now, I cant remember but I believe that if you add missing key data in
cleaned method, giving the name django would expect, you would be able
to just save the form. You could also just manually access the key
field from the cleaned_data and apply it on the model.


On 7 Mar., 18:13, Chris Spencer <chriss...@gmail.com> wrote:

Chris Spencer

unread,
Mar 7, 2009, 8:42:45 PM3/7/09
to django...@googlegroups.com
> Now, I cant remember but I believe that if you add missing key data in
> cleaned method, giving the name django would expect, you would be able
> to just save the form. You could also just manually access the key
> field from the cleaned_data and apply it on the model.

Unfortunately, this isn't the case. This was the first thing I tried,
but I found code in Django's forms/models.py that strips out cleaned
data that doesn't explicitly appear in either the fields or exclude
list. It's quite a frustrating and arbitrary limitation, and took me
several hours to work around.

Chris

Reply all
Reply to author
Forward
0 new messages