I got it working, thanks to your suggestion. Here is what I did:
# in forms.py
class SiteForm(forms.ModelForm):
class Meta:
model = Site
exclude = ('user',)
def __init__(self, user, *args, **kwargs):
self.user = user
super(SiteForm, self).__init__(*args, **kwargs)
def clean_url(self):
url = self.cleaned_data['url']
try:
Site.objects.get(user=self.user, url=url)
except Site.DoesNotExist:
pass
else:
raise forms.ValidationError(_('Site with this URL already
exists.'))
return url
# in views.py
@login_required
def site_add(request):
form = SiteForm(request.user, request.POST or None)
if form.is_valid():
site = form.save(commit=False)
site.user = request.user
site.save()
return redirect(...)
return render_to_response(...)
On Apr 18, 7:52 am, David Lindquist <[email protected]> wrote:
> Thanks Georg. I will give that a try.
>
> On Apr 18, 7:45 am, "[email protected]"
>
>
>
>
>
> <[email protected]> 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 <[email protected]> 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, "[email protected]"
>
> > > <[email protected]> wrote:
> > > > use commit=False
>
> > > > m = form.save(commit=False)
> > > > m.user = request.user
> > > > m.save()
>
> > > > On Apr 18, 6:06 am, David Lindquist <[email protected]> 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 [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected].
> > > > > 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 [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected].
> > > > 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 [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > 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 [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
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.