开发者

Cannot force an update in save() with no primary key. Horror !

i don't know what to do, i'm 'blank'...

I have a function that querys the database using a request.GET and fills the form, the you can change the contents and save updating the information. The problem is the id isn't passed to the function and Django opts for an INSERT instead of UPDATE, here's the code:

Look at this mess:

def m开发者_如何学Pythoneet_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 is supposed to work with save()
        #i tryed meeting.save(force_update=True)
        #and i figured out the id is missing
        meeting.save()
        for 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 with 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 an meeting=id passed in GET request (like /meeting/register/meeting=3) and the form gets the data but when saving it creates a new row instead of updating it,but it doesn't wirk like this in Django, maybe PHP but not Django !!!

This is the right code for Django:

def meet_save(request):
  pk=request.GET['meeting'] #get the meeteng id from 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'])# this is the id passed
    form = MeetingForm(instance=meeting)
  variables = RequestContext(request, {
        'form': form,
  })
  return render_to_response('meeting/meeting_save.html', variables)

I guess the mess maybe would work but i fixed my template because the POST was like this: method=POST action="." and i figured out the url was fine when i get to the page but when sending data in the post it clears out the meeting=id and i think that was the real Horror!! i changed it to action="" i have not seen anithing in the documentation about it and i have read it's the same, be aware it's not.

Django rocks !!!


Is there a reason why you aren't using the django built-in mechanism to fill the form with the associated object? For example:

meeting = Meeting.objects.get(pk=id)
form = MeetingForm(instance=meeting)

Also, you should use the django shortcut get_object_or_404 so that you can evade unwanted behavior. So, instead of creating a new object, it will just send back a 404. No harm done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜