Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

python - Editing related objects in ModelFormSet

I have these Models:

class Gallery(models.Model):
   HeadImage = models.ImageField(upload_to="gallery")

class Image(models.Model):
   Image = models.ImageField(upload_to="gallery")
   Gallery = models.ForeignKey(Gallery, related_name='images')

class Tour(models.Model):
   Name=models.CharField(max_length=100)
   Count=models.SmallIntegerField() 
   ActionUser=models.ForeignKey(User,editable=False)
   Gallery = models.OneToOneField(Gallery,editable=False)

as U can see,I have a form for tour and a form for gallery and a formset for gallerie's Images,in edittour view I wanna edit a Tour with it's gallery and gallerie's images.this is code to edit a Tour with it's related objects:

def edittour(request,key,tour_id):
   ImageFormSet = inlineformset_factory(Gallery,Image, can_delete=False,extra=4)
   tourinstance=Tour.objects.get(pk=tour_id)
   if request.method == 'POST':
      gform=GalleryForm(request.POST,request.FILES,instance=tourinstance.Gallery)
      if gform.is_valid():
         gallery=gform.save(commit=False)
         formset=ImageFormSet(request.POST, request.FILES, instance=tourinstance.Gallery)
         if formset.is_valid():
            gallery.save()
            formset.save()
         tform = TourForm(request.POST, request.FILES,instance=tourinstance)
         if tform.is_valid():
            tour=tform.save(commit=False)
            tour.ActionUserCode=User.objects.get(pk=1)
            tour.save()
            return render_to_response('airAgency/edittour.html', {'tform': tform,'gform':gform,'formset':formset})#'airAgency/edittour/%i/' % (tour.pk))
   else:
      pass
   tform = TourForm(instance=tourinstance)
   gform=GalleryForm(instance=tourinstance.Gallery)
   formset=ImageFormSet(instance=tourinstance.Gallery)
   return render_to_response('airAgency/edittour.html', {'tform': tform,'gform':gform,'formset':formset})

this works well when I edit Tour,but when I edit Image_set related to gallery it has this error:

MultiValueDictKeyError at /airAgency/mastane/edittour/1/

"Key 'images-0-id' not found in <QueryDict: {u'Count': [u'15'], u'images-TOTAL_FORMS': [u'5'], u'images-MAX_NUM_FORMS': [u''], u'HeadImage': [u''], u'Description': [u'-'], u'PriceUnitCode': [u'1'], u'images-3-Image': [u''], u'images-2-Image': [u''], u'Price': [u'15000000'], u'StatusTypeCode': [u'2'], u'images-INITIAL_FORMS': [u'1'], u'images-0-Image': [u''], u'csrfmiddlewaretoken': [u'e0a3aef084f11305a7610befda7cb27a'], u'images-4-Image': [u''], u'Name': [u'\u0645\u0627\u0644\u0632\u06cc']}>"

Request Method:     POST
Request URL:    http://127.0.0.1:8080/airAgency/mastane/edittour/1/
Django Version:     1.3
Exception Type:     MultiValueDictKeyError
Exception Value:    

"Key 'images-0-id' not found in <QueryDict: {u'Count': [u'15'], u'images-TOTAL_FORMS': [u'5'], u'images-MAX_NUM_FORMS': [u''], u'HeadImage': [u''], u'Description': [u'-'], u'PriceUnitCode': [u'1'], u'images-3-Image': [u''], u'images-2-Image': [u''], u'Price': [u'15000000'], u'StatusTypeCode': [u'2'], u'images-INITIAL_FORMS': [u'1'], u'images-0-Image': [u''], u'csrfmiddlewaretoken': [u'e0a3aef084f11305a7610befda7cb27a'], u'images-4-Image': [u''], u'Name': [u'\u0645\u0627\u0644\u0632\u06cc']}>"

Exception Location:     C:Python26libsite-packagesdjangoutilsdatastructures.py in __getitem__, line 256

...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you are manually rendering the form in your template, and missing out the id field. Make sure you include {{ form.id }} for each form in the formset.

<form method="post" action="">
    {{ formset.management_form }}
    {% for form in formset %}
        {{ form.id }}
        <ul>
            <li>{{ form.title }}</li>
        </ul>
    {% endfor %}
</form>

See the docs on using model formsets in the template for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...