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.
On Jul 27, 10:52 pm, AKK <andrewkenyon...@gmail.com> wrote: > 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. > > On 27 July, 12:35, Daniel Roseman <dan...@roseman.org.uk> wrote: > > > 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 -~----------~----~----~----~------~----~------~--~---