you could use modelforms, see
http://www.djangoproject.com/documentation/modelforms/

or without modelforms:

class MyForm(forms.Form):

    def __init__(self, user, *args, **kwargs):
        self.user = user
        super(MyForm, self).__init__(*args, **kwargs)

    ...
    define your fields here
    ...

    def save(self):
        new_object = Booking(user=self.user, ...)
        new_object.save()
        return new_object

and in your view:

form = MyForm(request.user, request.POST)


On Aug 21, 12:44 pm, "Genis Pujol Hamelink" <[EMAIL PROTECTED]>
wrote:
> Thanks for your reply,
>
> save() got an unexpected keyword argument 'commit'  as I didn't define
> commit in my save function... where did u get this commit=False from?
>
> Grtz,
>
> G.
>
>
>
> On Thu, Aug 21, 2008 at 12:41 PM, patrickk <[EMAIL PROTECTED]> wrote:
>
> > try this:
> > form.save(commit=False)
> > form.autor = request.user
> > form.save()
>
> > On Aug 21, 12:24 pm, "Genis Pujol Hamelink" <[EMAIL PROTECTED]>
> > wrote:
> > > Hello,
>
> > > I'm trying to pass a the id of the user logged in to a form field, but it
> > > doesn't seem to work... any ideas / suggestions?
>
> > > Thanks in advance :)
>
> > > Here are the view, model and form objects:
>
> > > View:
>
> > > def punts_form3(request):
>
> > >         if request.method == 'POST':
> > >               form = form_puntsn3(request.POST, request.FILES)
> > >               form.autor_id = request.session['_auth_user_id']  --> I get
> > an
> > > error message ""Column 'autor_id' cannot be null")"
> > >               if form.is_valid():
> > >                 form.save()
> > >                 return HttpResponseRedirect(".")
>
> > >         else:
>
> > >             form = form_puntsn3()
>
> > >         t = loader.get_template('htmls/punts_form3.html')
>
> > >         c = Context({
> > >               'form': form,
> > >           })
> > >         return HttpResponse(t.render(c))
>
> > > Model:
>
> > > class puntmanager(models.Manager):
> > >     def
> > create_punt(self,lat,lng,descr,erico,precisio,autor,quan,esvalid):
> > >         punt_nou =
> > > self.model(None,lat,lng,descr,erico,precisio,autor,quan,esvalid)
> > >         punt_nou.save()
> > >         return punt_nou
>
> > > class puntsn3(models.Model):
> > >     lat = models.FloatField(max_digits=16, decimal_places=14)
> > >     lng = models.FloatField(max_digits=16, decimal_places=14)
> > >     #imatge = models.ImageField(upload_to='img', blank=True, null=True)
> > >     descr = models.TextField('Desc', maxlength=500,blank=True, null=True)
> > >     erico = models.CharField(maxlength=1, choices=TIPUS_ERICONS)
> > >     precisio = models.CharField(maxlength=1, choices=NIV_PREC)
> > >     autor = models.ForeignKey(User)
> > >     quan = models.DateField()
> > >     esvalid = models.BooleanField(default='0',editable=False)
> > >     objects = puntmanager()
>
> > > class form_puntsn3(newforms.Form):
> > >     lat = newforms.IntegerField()#max_digits=16, decimal_places=14)
> > >     lng = newforms.IntegerField()#max_digits=16, decimal_places=14)
> > >     #imatge = newforms.Field(widget=newforms.FileInput())
> > >     descr = newforms.CharField()
> > >     erico = newforms.ChoiceField(choices=TIPUS_ERICONS)
> > >     precisio = newforms.ChoiceField(choices=NIV_PREC)
> > >     autor = newforms.IntegerField(required=False)
> > >     quan = newforms.DateField(initial='2008-01-01',
> > > widget=SelectDateWidget(years=range(2010, 2005, -1)))
> > >     esvalid = newforms.IntegerField(required=False)
> > >     def save(self):
> > >         nou_obj = puntsn3.objects.create_punt(lat=self.clean_data['lat'],
> > >                                         lng=self.clean_data['lng'],
> > >                                         descr=self.clean_data['descr'],
> > >                                         erico=self.clean_data['erico'],
>
> > > precisio=self.clean_data['precisio'],
> > >                                         autor=self.clean_data['autor'],
> > >                                         quan=self.clean_data['quan'],
>
> > esvalid=self.clean_data['esvalid']
> > >                                         )
> > >         return nou_obj
>
> > > --
> > > Genis
>
> --
> Genis
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to