List Info

Thread: Adding fields to form.clean_data




Adding fields to form.clean_data
country flaguser name
United States
2007-07-30 07:54:52
Hello!
I have a form generated with form_for_model.
I deleted a couple of fields from it, beacuse I don't want
the user to
insert value for them, but their value should be
automatically filled
after the submit of the form.

So, I have something like this:

MyForm = form_for_model(myModel)
del MyForm.base_fields['code']
del MyForm.base_fields['date']
del.MyForms.base_fields['author']
f = MyForm()

The form is nicely displayed the way I want.

Then, I have to save the data the user filled in the form,
adding
values for three fields I removed.
I tried to do this way:

f = MyForm(request.post)
if f.is_valid():
    num = Code.objects.get(pk=1) #I retrieve the code from a
value
stored in a model
    f.clean_data['code'] = num.nextcode
    f.clean_data['date'] = datetime.date.today()
    f.clean_data['author'] = request.user #I use the
standard django
authentication
    f.save()

The data are correctly saved on the database, but the three
particular
field behave strangely:
the 'code' field is correctly saved, but the 'author' and
'date' filed
are not: in the database, they are Null.
In the MyModel model, 'date' is a model.DateField object,
and 'author'
is a model.ForeignKey(User) object.

Am I doing something wrong?
Thanks.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Adding fields to form.clean_data
user name
2007-07-30 10:15:31
Hi,

if the fields will never be edited, you can use
editable=False
in your model.

I do it like this:

instance=formobj.save(commit=False)
instance.attribute=...
instance.save()

BTW: the "commit" keywords does not mean database
commit.
If means, don't call save() of the instance.

Am Montag, 30. Juli 2007 14:54 schrieb ilDave:
> Hello!
> I have a form generated with form_for_model.
> I deleted a couple of fields from it, beacuse I don't
want the user to
> insert value for them, but their value should be
automatically filled
> after the submit of the form.
>
> So, I have something like this:
>
> MyForm = form_for_model(myModel)
> del MyForm.base_fields['code']
> del MyForm.base_fields['date']
> del.MyForms.base_fields['author']
> f = MyForm()
>
> The form is nicely displayed the way I want.
>
> Then, I have to save the data the user filled in the
form, adding
> values for three fields I removed.
> I tried to do this way:
>
> f = MyForm(request.post)
> if f.is_valid():
>     num = Code.objects.get(pk=1) #I retrieve the code
from a value
> stored in a model
>     f.clean_data['code'] = num.nextcode
>     f.clean_data['date'] = datetime.date.today()
>     f.clean_data['author'] = request.user #I use the
standard django
> authentication
>     f.save()
>
> The data are correctly saved on the database, but the
three particular
> field behave strangely:
> the 'code' field is correctly saved, but the 'author'
and 'date' filed
> are not: in the database, they are Null.
> In the MyModel model, 'date' is a model.DateField
object, and 'author'
> is a model.ForeignKey(User) object.
>
> Am I doing something wrong?
> Thanks.
>
>
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Adding fields to form.clean_data
country flaguser name
United States
2007-07-30 11:12:20
Thank you, I'll try with the commit=False! 

On Jul 30, 5:15 pm, Thomas Guettler <h...tbz-pariv.de> wrote:
> Hi,
>
> if the fields will never be edited, you can use
editable=False
> in your model.
>
> I do it like this:
>
> instance=formobj.save(commit=False)
> instance.attribute=...
> instance.save()
>
> BTW: the "commit" keywords does not mean
database commit.
> If means, don't call save() of the instance.
>
> Am Montag, 30. Juli 2007 14:54 schrieb ilDave:
>
> > Hello!
> > I have a form generated with form_for_model.
> > I deleted a couple of fields from it, beacuse I
don't want the user to
> > insert value for them, but their value should be
automatically filled
> > after the submit of the form.
>
> > So, I have something like this:
>
> > MyForm = form_for_model(myModel)
> > del MyForm.base_fields['code']
> > del MyForm.base_fields['date']
> > del.MyForms.base_fields['author']
> > f = MyForm()
>
> > The form is nicely displayed the way I want.
>
> > Then, I have to save the data the user filled in
the form, adding
> > values for three fields I removed.
> > I tried to do this way:
>
> > f = MyForm(request.post)
> > if f.is_valid():
> >     num = Code.objects.get(pk=1) #I retrieve the
code from a value
> > stored in a model
> >     f.clean_data['code'] = num.nextcode
> >     f.clean_data['date'] = datetime.date.today()
> >     f.clean_data['author'] = request.user #I use
the standard django
> > authentication
> >     f.save()
>
> > The data are correctly saved on the database, but
the three particular
> > field behave strangely:
> > the 'code' field is correctly saved, but the
'author' and 'date' filed
> > are not: in the database, they are Null.
> > In the MyModel model, 'date' is a model.DateField
object, and 'author'
> > is a model.ForeignKey(User) object.
>
> > Am I doing something wrong?
> > Thanks.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )