Thanks Georg. I will give that a try.

On Apr 18, 7:45 am, "ge...@aquarianhouse.com"
<ge...@aquarianhouse.com> wrote:
> ok now i got it :)
>
> i would do this:
>
> class SiteForm(forms.ModelForm):
>
>     def __init__(user, *args, **kwargs):
>        self.user = user
>        super(SiteForm, self).__init__(*args, **kwargs)
>
>     class Meta:
>         model = Site
>         exclude = ('user',)
>
>     def clean_url(self):
>        #check here and give an error...
>
> form = SiteForm(request.user, request.POST)
>
> m = form.save(commit=False)
> m.user = request.user
> m.save()
>
> something like this :)
>
> On Apr 18, 4:32 pm, David Lindquist <david.lindqu...@gmail.com> wrote:
>
>
>
>
>
> > Thanks for the reply.
>
> > The problem I have with that solution is that it occurs after form
> > validation takes place. Notice that in my Site model the url and user
> > fields are specified as being unique_together. If I set the user as
> > you suggest, I still run the risk of a database error.
>
> > Is there another way to accomplish this?
>
> > On Apr 18, 3:14 am, "ge...@aquarianhouse.com"
>
> > <ge...@aquarianhouse.com> wrote:
> > > use commit=False
>
> > > m = form.save(commit=False)
> > > m.user = request.user
> > > m.save()
>
> > > On Apr 18, 6:06 am, David Lindquist <david.lindqu...@gmail.com> wrote:
>
> > > > Greetings,
>
> > > > I am trying to solve what seems like an easy problem, but the solution
> > > > eludes me even after many Google searches.
>
> > > > I have a simple model:
>
> > > > class Site(models.Model):
> > > >     user = models.ForeignKey(User)
> > > >     url = models.URLField()
>
> > > >     class Meta:
> > > >         unique_together = (('user', 'url'),)
>
> > > > And an equally simple ModelForm:
>
> > > > class SiteForm(forms.ModelForm):
> > > >     class Meta:
> > > >         model = Site
> > > >         exclude = ('user',)
>
> > > > I exclude the user field because I want to be able to save a Site
> > > > object for the currently logged in user. But no matter how I try to
> > > > set the user in the view, form.save() raises an IntegrityError
> > > > ("Column 'user_id' cannot be null").
>
> > > > I know I could include the user field and generate the form with a
> > > > hidden field using the pk of the current user, but that seems like it
> > > > could be easily altered.
>
> > > > What is the best approach for this?
>
> > > > --
> > > > You received this message because you are subscribed to the Google 
> > > > Groups "Django users" group.
> > > > To post to this group, send email to django-us...@googlegroups.com.
> > > > To unsubscribe from this group, send email to 
> > > > django-users+unsubscr...@googlegroups.com.
> > > > For more options, visit this group 
> > > > athttp://groups.google.com/group/django-users?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups 
> > > "Django users" group.
> > > To post to this group, send email to django-us...@googlegroups.com.
> > > To unsubscribe from this group, send email to 
> > > django-users+unsubscr...@googlegroups.com.
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/django-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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