post - Cannot force an update in save() with no primary key. Horror ! -
i don't know do, i'm 'blank'...
i have function querys database using request.get , fills form, can change contents , save updating information. problem id isn't passed function , django opts insert instead of update, here's code:
look @ mess:
def meet_save(request): if request.method == 'post': form = meetingform(request.post) if form.is_valid(): creator = get_object_or_404(submitter, name=request.user) meeting = meeting(creator=creator) meeting.ave = form.cleaned_data['ave'] meeting.st = form.cleaned_data['st'] meeting.date = form.cleaned_data['date'] #it supposed work save() #i tryed meeting.save(force_update=true) #and figured out id missing meeting.save() people in form.cleaned_data['people']: meeting.people.add(people) return httpresponseredirect('/meeting/register/') elif request.get.has_key('meeting'): id = request.get['meeting'] people = '' ave = '' st = '' date = '' try: meeting = meeting.objects.get(id=id) #id = unicode(request.get['meeting']) #have tryed different ways no luck id = request.get['meeting'] ave = meeting.ave st = meeting.st date = meeting.date except: pass form = meetingform({ 'id':id, 'ave' : ave, 'st' : st,'date' : date }) else: form = meetingform() variables = requestcontext(request, { 'form': form, }) return render_to_response('meeting/meeting_save.html', variables)
it receives meeting=id passed in request (like /meeting/register/meeting=3) , form gets data when saving creates new row instead of updating it,but doesn't wirk in django, maybe php not django !!!
this right code django:
def meet_save(request): pk=request.get['meeting'] #get meeteng id url form = meetingform(request.post) if form.is_valid(): meeting, created = meeting.objects.get_or_create(id=pk) meeting.ave = form.cleaned_data['ave'] meeting.st = form.cleaned_data['st'] meeting.date = form.cleaned_data['date'] meeting.people = form.claned_data['people'] meeting.save() #for people in form.cleaned_data['people']: #meeting.people.add(people) return httpresponseredirect('/meeting/register/') else: meeting = get_object_or_404(meeting, pk=request.get['meeting'])# id passed form = meetingform(instance=meeting) variables = requestcontext(request, { 'form': form, }) return render_to_response('meeting/meeting_save.html', variables)
i guess mess
maybe work fixed template because post this: method=post action="." , figured out url fine when page when sending data in post clears out meeting=id , think real horror!! changed action="" have not seen anithing in documentation , have read it's same, aware it's not.
django rocks !!!
is there reason why aren't using django built-in mechanism fill form associated object? example:
meeting = meeting.objects.get(pk=id) form = meetingform(instance=meeting)
also, should use django shortcut get_object_or_404 can evade unwanted behavior. so, instead of creating new object, send 404. no harm done.
Comments
Post a Comment