Thursday, May 31, 2012

Re: Adding values to formdata of ModelForm before saving

Thank you for your quick answer, but it doesn't really work. It raises the exception "can't assign to operator"
My view now looks like this:

@login_required
def create_termin(request, calid, year=None, month=None, day=None):
    kalender = get_object_or_404(Kalender,pk=int(calid))
    if request.method == "POST":
        print(request.POST)
        form = NewTerminForm(request.POST)

        if form.is_valid():
            formdata = form.cleaned_data
            if formdata['participants']:
                form.participants = request.user.id
            form.save(commit=False)
            form.in_calendar-id = kalender
            form.save()
            return HttpResponse(pprint.pformat(formdata) + pprint.pformat(inspect.getmembers(form['participants'])) + "\n\n" + pprint.pformat(inspect.getmembers(request.user)),mimetype="text/plain")

    else:
        date = datetime.date(month=int(month), day=int(day), year=int(year)) if day else None
        print("viewdate",date)
        form = NewTerminForm(initial={'date':date,})
    return render_to_response("kalender/new_termin.html", dict(debug=1, calid=calid,form=form, ),context_instance=RequestContext(request))

Am Mittwoch, 30. Mai 2012 23:03:49 UTC+2 schrieb jondbaker:
Yes, it is possible:

obj = form.save(commit=False)
obj.in_calender-id = kalender
obj.save()


On Wed, May 30, 2012 at 2:56 PM, Schmidtchen Schleicher <spiollinux@googlemail.com> wrote:
Is it possible to put missing data into the data of a form before calling form.save() ?
I want to add the calendar-id (calid/in_calendar) before saving to the model.

 http://susepaste.org/23451355

I tried adding it after instantiating the form with form.in_calendar_id = kalender but it didn't work
I don't want to use hidden fields because manipulating them is easy

Maybe https://groups.google.com/forum/?fromgroups#!topic/django-users/F5yHH-G5QLM describes how to solve it but I didn't understand it.

PS: If you find very ugly code please correct me -- I'm a beginner

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/v0GYTeLYPAsJ.
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.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/bAQHJsJ7lkcJ.
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