Sunday, March 27, 2011

Re: Integrity Error on chained FKs

On Sunday, March 27, 2011 9:41:53 AM UTC+1, shofty wrote:
this is the view code...

    u = get_object_or_404(UserProfile, user=request.user)
    if request.method == "POST":
        # submitted the add venue form
        venue_form = VenueForm(request.POST)
        venue_form.user = u
        venue_form.slug = slugify(request.POST['name'])
        if venue_form.is_valid():
            venue_form.save()


The lines starting with `venue_form.user` and `venue_form.slug` aren't doing what you intend. They aren't setting the value of the form's fields to anything - they are just setting arbitrary attributes on the form, which are ignored.

What you mean to do is this:

        venue_form = VenueForm(request.POST) 
        if venue_form.is_valid(): 
            venue = venue_form.save(commit=False) 
            venue.user = u 
            venue.slug = slugify(venue.name) 
            venue.save()

To make this work, you'll probably need to add the user and slug fields to the form's `exclude` tuple.

I don't know what this has to do with "chained FKs", though.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment