how to save a forms.ModelForm with extra fields?

496 views
Skip to first unread message

jul

unread,
Nov 12, 2009, 5:33:24 PM11/12/09
to Django users
Hi

I've got the Rating model and the AddRestaurantForm shown below. In
order to add a rating when submitting a new restaurant, I added an
extra field to AddRestaurantForm. Can I do that? If I can, how can I
save separately the Restaurant instance and the rating instance (I'll
get the user from the context)?

thanks
jul

class Rating(models.Model):

user = models.ForeignKey(User)
restaurant = models.ForeignKey(Restaurant)
rating = models.PositiveSmallIntegerField()


class AddRestaurantForm(ModelForm):

rating = models.IntegerField()

class Meta:
model = Restaurant




Andy Mckay

unread,
Nov 12, 2009, 11:35:06 PM11/12/09
to django...@googlegroups.com
On 09-11-12 2:33 PM, jul wrote:
> I've got the Rating model and the AddRestaurantForm shown below. In
> order to add a rating when submitting a new restaurant, I added an
> extra field to AddRestaurantForm. Can I do that? If I can, how can I
> save separately the Restaurant instance and the rating instance (I'll
> get the user from the context)?

Sure that works just fine.

When you save the form, you'll save the restaurant instance. You can
then get the rating from the forms.cleaned_data and save that however
you'd like.
--
Andy McKay
@clearwind
Training: http://clearwind.ca/training/
Zen: http://djangozen.com

Hector Garcia

unread,
Nov 13, 2009, 5:16:24 AM11/13/09
to django...@googlegroups.com
You have a rating field in your model, I don't know if it is
intentionally but then in your ModelForm, if you specify an extra
field also named 'rating' then you are overriding the one in the
model, I think. Also, such extra field in the form, shouldn't it be
form.IntegerField instead of model.IntegerField?

Hector Garcia - Web developer, musician
http://nomadblue.com/
> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=.
>
>
>

jul

unread,
Nov 13, 2009, 5:39:47 AM11/13/09
to Django users
yes you're right it should be rating = models.IntegerField().
In my Restaurant model there is no rating field, so it's not
overrided.
thanks


On Nov 13, 11:16 am, Hector Garcia <hecto...@gmail.com> wrote:
> You have a rating field in your model, I don't know if it is
> intentionally but then in your ModelForm, if you specify an extra
> field also named 'rating' then you are overriding the one in the
> model, I think. Also, such extra field in the form, shouldn't it be
> form.IntegerField instead of model.IntegerField?
>
> Hector Garcia - Web developer, musicianhttp://nomadblue.com/

jul

unread,
Nov 13, 2009, 5:44:28 AM11/13/09
to Django users
I meant rating = forms.IntegerField()

On Nov 13, 11:16 am, Hector Garcia <hecto...@gmail.com> wrote:
> You have a rating field in your model, I don't know if it is
> intentionally but then in your ModelForm, if you specify an extra
> field also named 'rating' then you are overriding the one in the
> model, I think. Also, such extra field in the form, shouldn't it be
> form.IntegerField instead of model.IntegerField?
>
> Hector Garcia - Web developer, musicianhttp://nomadblue.com/

jul

unread,
Nov 13, 2009, 5:59:22 AM11/13/09
to Django users
What happen to the rating field when doing the following?

f = AddRestaurantForm(request.POST)
f.save()

Does save() only use what it needs to fill the Restaurant instance and
doesn't use the rating value?

Hector Garcia

unread,
Nov 13, 2009, 7:42:49 AM11/13/09
to django...@googlegroups.com
Exactly, it ignores whatever POST value not related to the model
field. You have to use f.cleaned_data['rating'] to get the rating
value and use it or save it wherever it has to be stored, as Andy
McKay pointed in his comment.

Hector Garcia - Web developer, musician
http://nomadblue.com/



jul

unread,
Nov 13, 2009, 9:17:28 AM11/13/09
to Django users
great. Thank you!

On Nov 13, 1:42 pm, Hector Garcia <hecto...@gmail.com> wrote:
> Exactly, it ignores whatever POST value not related to the model
> field. You have to use f.cleaned_data['rating'] to get the rating
> value and use it or save it wherever it has to be stored, as Andy
> McKay pointed in his comment.
>
> Hector Garcia - Web developer, musicianhttp://nomadblue.com/
Reply all
Reply to author
Forward
0 new messages