我有模特学院,可以有照片和医生。所以我使用inlineformset_factory
来实现"创建机构"功能。
因此,如果我添加两张或更多照片或医生 - 表单只保存最后一张。为什么会发生这种情况以及如何解决这个问题?
models.py
class Institution(models.Model):
name = models.CharField(max_length=255)
class Meta:
verbose_name = u"Institution"
verbose_name_plural = u"Institutions"
def __unicode__(self):
return self.name
class Photo(models.Model):
photo = ResizedImageField(size=[600, 600], crop=['middle', 'center'], quality=90, upload_to='institution_photos')
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Photo"
verbose_name_plural = u"Photos"
class Doctor(models.Model):
name = models.CharField(max_length=255)
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Doctor"
verbose_name_plural = u"Doctors"
def __unicode__(self):
return self.name
forms.py
class InstitutionForm(forms.ModelForm):
class Meta:
model = Institution
fields = '__all__'
error_css_class = 'error'
required_css_class = 'required'
def __init__(self, *args, **kwargs):
super(InstitutionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
class PhotoForm(forms.ModelForm):
class Meta:
model = Photo
fields = '__all__'
error_css_class = 'error'
required_css_class = 'required'
def __init__(self, *args, **kwargs):
super(PhotoForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = True
class DoctorForm(forms.ModelForm):
class Meta:
model = Doctor
fields = '__all__'
error_css_class = 'error'
required_css_class = 'required'
def __init__(self, *args, **kwargs):
super(DoctorForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = True
InstitutionPhotosFromSet = inlineformset_factory(Institution, Photo, PhotoForm, extra=1)
InstitutionDoctorsFromSet = inlineformset_factory(Institution, Doctor, DoctorForm, extra=1)
views.py
def institution_create(request):
if request.POST:
form = InstitutionForm(request.POST, request.FILES)
if form.is_valid():
print 'a form is valid!'
institution = form.save(commit=False)
photo_form = InstitutionPhotosFromSet(request.POST, request.FILES, prefix='photos', instance=institution)
doctor_form = InstitutionDoctorsFromSet(request.POST, request.FILES, prefix='doctors', instance=institution)
print photo_form
if photo_form.is_valid():
print 'a photo_form is valid!'
photo_form.save()
if doctor_form.is_valid():
print 'a doctor_form is valid!'
doctor_form.save()
institution.save()
return redirect('base:institutions_list')
else:
form = InstitutionForm()
photo_form = InstitutionPhotosFromSet(prefix='photos')
doctor_form = InstitutionDoctorsFromSet(prefix='doctors')
args = {}
args.update(csrf(request))
args['form'] = form
args['photo_form'] = photo_form
args['doctor_form'] = doctor_form
full_name = request.user.username
return render(request, 'base/create_institution.html', locals())
模板
<form class="the_form" enctype="multipart/form-data" action="." method="post">
{% csrf_token %}
{{ form|crispy }}
{% wysiwyg_editor "id_services" %}
{% wysiwyg_editor "id_license_text" %}
{% wysiwyg_editor "id_schedule" %}
<fieldset>
<legend>Photos</legend>
{{ photo_form.management_form|crispy }}
{{ photo_form.non_form_errors|crispy }}
{% for form in photo_form %}
{{ form.id }}
<div class="inline {{ doctor_form.prefix }}">
{{ form|crispy }}
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Doctors</legend>
{{ doctor_form.management_form|crispy }}
{{ doctor_form.non_form_errors|crispy }}
{% for form in doctor_form %}
{{ form.id }}
<div class="inline {{ photo_form.prefix }}">
{{ form|crispy }}
</div>
{% endfor %}
</fieldset>
<div class="text-center">
<input class="btn btn-default" type="submit" name="submit" value="Save"/>
<a class="btn btn-danger" href="/home/">Cancel</a>
</div>
</form>
可能会有所帮助。当我打印photo_form时,它会打印我:
<input id="id_photos-TOTAL_FORMS" name="photos-TOTAL_FORMS" type="hidden" value="1" /><input id="id_photos-INITIAL_FORMS" name="photos-INITIAL_FORMS" type="hidden" value="0" /><input id="id_photos-MIN_NUM_FORMS" name="photos-MIN_NUM_FORMS" type="hidden" value="0" /><input id="id_photos-MAX_NUM_FORMS" name="photos-MAX_NUM_FORMS" type="hidden" value="1000" /> <tr><th><label for="id_photos-0-photo">Photo:</label></th><td><input id="id_photos-0-photo" name="photos-0-photo" type="file" /></td></tr> <tr><th><label for="id_photos-0-DELETE">Delete:</label></th><td><input id="id_photos-0-DELETE" name="photos-0-DELETE" type="checkbox" /><input id="id_photos-0-rel_institution" name="photos-0-rel_institution" type="hidden" /><input id="id_photos-0-id" name="photos-0-id" type="hidden" /></td></tr>
答案 0 :(得分:0)
这个问题的答案非常“神奇”,取决于我的another question。
要让内联formset保存多个记录,html应更改为以下内容:
<fieldset>
<legend>Photos</legend>
{{ photo_form.management_form|crispy }}
{{ photo_form.non_form_errors|crispy }}
{% for form in photo_form %}
{{ form.id }}
<div class="inline {{ photo_form.prefix }}">
{{ form|crispy }}
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Doctors</legend>
{{ doctor_form.management_form|crispy }}
{{ doctor_form.non_form_errors|crispy }}
{% for form in doctor_form %}
{{ form.id }}
<div class="inline {{ doctor_form.prefix }}">
{{ form|crispy }}
</div>
{% endfor %}
</fieldset>