Editing model based forms.

0 views
Skip to first unread message

AKK

unread,
Jul 26, 2009, 7:15:04 PM7/26/09
to Django users
Hi, I have a form based on a model which allows users to add comment.
A comment is made up of the comment is about, the body, the date and
time and also who made the comment.

I am mainly interested in the datetime and user fields. What i would
like to know is how i can automatically have these entered. For the
username i know if i created the form myself i could have a hidden
field with the value of {{ user.username }}. I'm not sure how i could
automatically specify the time. But since the form is created
automatically i could do with some help.

Thanks,

Andrew

AKK

unread,
Jul 27, 2009, 6:52:04 AM7/27/09
to Django users
Is there a way to do this or should i create my own form manually?

Andrew

Daniel Roseman

unread,
Jul 27, 2009, 7:35:57 AM7/27/09
to Django users
On Jul 27, 12:15 am, AKK <AndrewKenyon...@gmail.com> wrote:
If you want values to be entered without user interaction, then
there's no point in having them in the form at all. Use the 'exclude'
property in the form's Meta class to exclude user, date and time. Then
in your view, when you save the form use commit=False, then add the
values you want to the model instance before saving.

...
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.datetime = datetime.datetime.now()
instance.save()

Alternatively, you might want to use the auto_now_add parameter in the
model definition for the datetime field.
--
DR

AKK

unread,
Jul 27, 2009, 3:52:13 PM7/27/09
to Django users
Thanks, that will work for time.

I want the username value to be added without the user typing it in
because they can type anything and its a bit silly to get them
to type in there username everytime they want to comment. So i want
the {{ user.username }} value to be automatically detected and entered
but i don't know if you can do this with a model based form or if you
have to manually create it.

Ramiro Morales

unread,
Jul 27, 2009, 5:46:21 PM7/27/09
to django...@googlegroups.com

The Django documentation set contains a [1]document
titled "Creating forms from models" completely devoted to
the topic. I'd recommend to read it if you haven't done so
yet and if you are in a hurry as you seems to be.

That document shows, somewhat prominently, a
[2]section named "Using a subset of fields on the form"
that describes the techniques (and the relevant
caveats) for doing what you describe.

1. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
2. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

HTH

--
Ramiro Morales
http://rmorales.net

PyCon 2009 Argentina - Vie 4 y Sab 5 Septiembre
Buenos Aires, Argentina
http://ar.pycon.org/2009/about/

Eugene Mirotin

unread,
Jul 28, 2009, 3:32:55 AM7/28/09
to Django users
AKK, the previous response from Daniel Roseman completely answers your
question - the user field is HIDDEN on the form and AUTOMATICALLY
filled in the form handling action.
You should read the Django 'topics', I think.
Reply all
Reply to author
Forward
0 new messages