问题描述
我有以下模型:
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)
如您所见,我有一个用于游览的表单,一个用于画廊的表单以及一个用于画廊图像的表单集,在edittour视图中,我想用它的画廊和画廊图像编辑一个Tour。编辑带有相关对象的游览:
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})
在编辑Tour时效果很好,但是当我编辑与图库相关的Image_set时出现此错误:
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:\Python26\lib\site-packages\django\utils\datastructures.py in __getitem__, line 256
...
推荐答案
您正在模板中手动呈现表单,并且缺少id字段。确保在表单集中为每个表单包括 {{form.id}}
。
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.
这篇关于在ModelFormSet中编辑相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!