gpt4 book ai didi

python - 编辑ModelFormSet中的相关对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:29 26 4
gpt4 key购买 nike

我有这些模型:

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 View 中,我想用它的画廊和画廊图像编辑一个游览。这是用它编辑一个游览的代码相关对象:

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 时效果很好,但是当我编辑与 gallery 相关的 Image_set 时出现此错误:

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 }}

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

请参阅 using model formsets in the template 上的文档获取更多信息。

关于python - 编辑ModelFormSet中的相关对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com