On Jul 27, 12:15 am, AKK <andrewkenyon...@gmail.com> wrote:
> 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

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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to