Tuesday, September 27, 2011

Re: commit=false and user id

On Tue, Sep 27, 2011 at 3:55 PM, David <cthlogs@gmail.com> wrote:
> Hopefully this will make sense.
>
> You are on a page displaying information about a Person. You want to
> add a log to this person so you fill in the form at the bottom of the
> page. When that form is saved to a log object both the user that has
> submitted the log, and the person to whom the log belongs get saved.
>
> That is what I am trying to do.
>
> My log class:
>
> class Log(models.Model):
>    person = models.ForeignKey(Person)
>    user = models.ForeignKey(User, verbose_name="team member")
>    contact_date = models.DateTimeField(auto_now_add=True)
>    contact_detail = models.TextField()
>    modification_date = models.DateTimeField(auto_now=True)
>
> My log_update view:
>
> @login_required
> @transaction.commit_on_success
> def update_log(request):
>    form = LogForm(request.POST)
>    if form.is_valid():
>        f = form.save(commit=False)
>        f.user = request.user
>        f.person = request.person
>        f.save()
>        response_dict = {}
>        response_dict.update({'success': True})
>        return HttpResponse(json.dumps(response_dict),
> mimetype="application/json")
>    else:
>        return HttpResponse(json.dumps(form.errors),
> mimetype="application/json")
>
> The only form visible on my Person page is contact_detail. I need both
> the user.id and person.id to be saved with my form. So I've tried
> using commit=False to fill in the missing fields that would prevent
> the save from occurring (required fields) but I keep getting 500
> server errors because of the missing fields.

Are you 100% sure that you get them 'because of the missing fields'
(traceback or it never happened). I'm curious as to how
'request.person' gets populated - are you sure you aren't getting an
AttributeError when it accesses request.person?

Cheers

Tom

--
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