内联表单集添加了错误的表单
Inline formset adding wrong form
我有一个表单和两个内联表单集。
当我想添加 photo_form
时实际上添加了 doctors_form
,但是如果我在 html 中交换 photo_form
字段集和 doctor_form
字段集 - 一切正常!发生了什么魔法?!
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
InstitutionDoctorsFormSet = inlineformset_factory(Institution, Doctor, form=InstitutionForm, extra=1)
InstitutionPhotosFromSet = inlineformset_factory(Institution, Photo, form=InstitutionForm, extra=1)
views.py
def institution_create(request):
if request.POST:
form = InstitutionForm(request.POST, request.FILES)
doctor_form = InstitutionDoctorsFormSet(request.POST, request.FILES)
photo_form = InstitutionPhotosFormSet(request.POST, request.FILES)
if form.is_valid() and doctor_form.is_valid() and photo_form.is_valid():
form.save()
doctor_form.instance = form.save()
doctor_form.save()
photo_form.instance = form.save()
photo_form.save()
return redirect('base:institutions_list')
else:
form = InstitutionForm()
doctor_form = InstitutionDoctorsFormSet()
photo_form = InstitutionPhotosFormSet()
args = {}
args.update(csrf(request))
args['form'] = form
args['doctor_form'] = doctor_form
args['photo_form'] = photo_form
full_name = request.user.username
return render(request, 'base/create_institution.html', locals())
html
<form class="the_form" enctype="multipart/form-data" action="." method="post">
{% csrf_token %}
{% crispy form %}
{% wysiwyg_editor "id_services" %}
{% wysiwyg_editor "id_license_text" %}
{% wysiwyg_editor "id_schedule" %}
<fieldset>
<legend>Photos</legend>
{{ photo_form.management_form }}
{{ photo_form.non_form_errors }}
{% for form in photo_form %}
{{ form.id }}
<div class="inline {{ photo_form.prefix }}">
{% crispy form %}
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Doctors</legend>
{{ doctor_form.management_form }}
{{ doctor_form.non_form_errors }}
{% for form in doctor_form %}
{{ form.id }}
<div class="inline {{ doctor_form.prefix }}">
{% crispy form %}
<br/>
<br/>
</div>
{% endfor %}
</fieldset>
<div class="text-center">
<input class="btn btn-default" type="submit" name="submit" value="Создать"/>
<a class="btn btn-danger" href="/home/">Cancel</a>
</div>
</form>
我试图删除 crispy 形式的用法,但一切都保持不变。
更新:
class Institution(models.Model):
category = models.ForeignKey(Category, verbose_name="Категория")
logo = ResizedImageField(size=[320, 320], crop=['middle', 'center'], quality=90, upload_to='logos', verbose_name="Логотип")
name = models.CharField(max_length=255, verbose_name="Название")
short_description = models.CharField(max_length=255, verbose_name="Описание")
services = models.TextField(verbose_name="Услуги")
license_text = models.TextField(blank=True, verbose_name="Лицензия")
license_photo = models.ImageField(blank=True, verbose_name="Фото лицензии")
actions = models.CharField(max_length=255, verbose_name="Акции")
schedule = models.TextField(max_length=255, verbose_name="Расписание")
phone = models.CharField(max_length=255, verbose_name="Телефон")
email = models.CharField(max_length=255, verbose_name="Почта")
address = models.CharField(max_length=255, verbose_name="Адрес")
coordinates = models.CharField(default="", max_length=255, verbose_name="Координаты")
website = models.CharField(max_length=255, verbose_name="Сайт")
class Meta:
verbose_name = u"Заведение"
verbose_name_plural = u"Заведения"
def __unicode__(self):
return self.name
class Photo(models.Model):
photo = ResizedImageField(size=[600, 600], crop=['middle', 'center'], quality=90, upload_to='institution_photos', verbose_name="Фото")
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Фото"
verbose_name_plural = u"Фото"
class Doctor(models.Model):
name = models.CharField(max_length=255, verbose_name="ФИО")
doctor_photo = ResizedImageField(size=[400, 600], crop=['middle', 'center'], quality=90, upload_to='doctors_photos', verbose_name="Фото")
specs = models.CharField(max_length=1000, verbose_name="Специализация")
experience = models.CharField(max_length=255, verbose_name="Стаж работы")
regime = models.TextField(default="", verbose_name="Режим работы")
doctor_to_home = models.BooleanField(verbose_name="Возможность вызова на дом")
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Врач"
verbose_name_plural = u"Врачи"
def __unicode__(self):
return self.name
if form.is_valid() and doctor_form.is_valid() and photo_form.is_valid():
obj = form.save()
doctor_form.instance = obj
doctor_form.save()
photo_form.instance = obj
photo_form.save()
return redirect('base:institutions_list')
我发现了一个问题。这是在脚本中。没有 formCssClass
参数。
所以脚本应该是这样的:
<script type="text/javascript">
$(function () {
$(".inline.{{ doctor_form.prefix }}").formset({
prefix: "{{ doctor_form.prefix }}",
formCssClass: 'dynamic-formset1'
});
$(".inline.{{ photo_form.prefix }}").formset({
prefix: "{{ photo_form.prefix }}",
formCssClass: 'dynamic-formset2'
})
})
</script>
我有一个表单和两个内联表单集。
当我想添加 photo_form
时实际上添加了 doctors_form
,但是如果我在 html 中交换 photo_form
字段集和 doctor_form
字段集 - 一切正常!发生了什么魔法?!
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
InstitutionDoctorsFormSet = inlineformset_factory(Institution, Doctor, form=InstitutionForm, extra=1)
InstitutionPhotosFromSet = inlineformset_factory(Institution, Photo, form=InstitutionForm, extra=1)
views.py
def institution_create(request):
if request.POST:
form = InstitutionForm(request.POST, request.FILES)
doctor_form = InstitutionDoctorsFormSet(request.POST, request.FILES)
photo_form = InstitutionPhotosFormSet(request.POST, request.FILES)
if form.is_valid() and doctor_form.is_valid() and photo_form.is_valid():
form.save()
doctor_form.instance = form.save()
doctor_form.save()
photo_form.instance = form.save()
photo_form.save()
return redirect('base:institutions_list')
else:
form = InstitutionForm()
doctor_form = InstitutionDoctorsFormSet()
photo_form = InstitutionPhotosFormSet()
args = {}
args.update(csrf(request))
args['form'] = form
args['doctor_form'] = doctor_form
args['photo_form'] = photo_form
full_name = request.user.username
return render(request, 'base/create_institution.html', locals())
html
<form class="the_form" enctype="multipart/form-data" action="." method="post">
{% csrf_token %}
{% crispy form %}
{% wysiwyg_editor "id_services" %}
{% wysiwyg_editor "id_license_text" %}
{% wysiwyg_editor "id_schedule" %}
<fieldset>
<legend>Photos</legend>
{{ photo_form.management_form }}
{{ photo_form.non_form_errors }}
{% for form in photo_form %}
{{ form.id }}
<div class="inline {{ photo_form.prefix }}">
{% crispy form %}
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Doctors</legend>
{{ doctor_form.management_form }}
{{ doctor_form.non_form_errors }}
{% for form in doctor_form %}
{{ form.id }}
<div class="inline {{ doctor_form.prefix }}">
{% crispy form %}
<br/>
<br/>
</div>
{% endfor %}
</fieldset>
<div class="text-center">
<input class="btn btn-default" type="submit" name="submit" value="Создать"/>
<a class="btn btn-danger" href="/home/">Cancel</a>
</div>
</form>
我试图删除 crispy 形式的用法,但一切都保持不变。
更新:
class Institution(models.Model):
category = models.ForeignKey(Category, verbose_name="Категория")
logo = ResizedImageField(size=[320, 320], crop=['middle', 'center'], quality=90, upload_to='logos', verbose_name="Логотип")
name = models.CharField(max_length=255, verbose_name="Название")
short_description = models.CharField(max_length=255, verbose_name="Описание")
services = models.TextField(verbose_name="Услуги")
license_text = models.TextField(blank=True, verbose_name="Лицензия")
license_photo = models.ImageField(blank=True, verbose_name="Фото лицензии")
actions = models.CharField(max_length=255, verbose_name="Акции")
schedule = models.TextField(max_length=255, verbose_name="Расписание")
phone = models.CharField(max_length=255, verbose_name="Телефон")
email = models.CharField(max_length=255, verbose_name="Почта")
address = models.CharField(max_length=255, verbose_name="Адрес")
coordinates = models.CharField(default="", max_length=255, verbose_name="Координаты")
website = models.CharField(max_length=255, verbose_name="Сайт")
class Meta:
verbose_name = u"Заведение"
verbose_name_plural = u"Заведения"
def __unicode__(self):
return self.name
class Photo(models.Model):
photo = ResizedImageField(size=[600, 600], crop=['middle', 'center'], quality=90, upload_to='institution_photos', verbose_name="Фото")
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Фото"
verbose_name_plural = u"Фото"
class Doctor(models.Model):
name = models.CharField(max_length=255, verbose_name="ФИО")
doctor_photo = ResizedImageField(size=[400, 600], crop=['middle', 'center'], quality=90, upload_to='doctors_photos', verbose_name="Фото")
specs = models.CharField(max_length=1000, verbose_name="Специализация")
experience = models.CharField(max_length=255, verbose_name="Стаж работы")
regime = models.TextField(default="", verbose_name="Режим работы")
doctor_to_home = models.BooleanField(verbose_name="Возможность вызова на дом")
rel_institution = models.ForeignKey(Institution)
class Meta:
verbose_name = u"Врач"
verbose_name_plural = u"Врачи"
def __unicode__(self):
return self.name
if form.is_valid() and doctor_form.is_valid() and photo_form.is_valid():
obj = form.save()
doctor_form.instance = obj
doctor_form.save()
photo_form.instance = obj
photo_form.save()
return redirect('base:institutions_list')
我发现了一个问题。这是在脚本中。没有 formCssClass
参数。
所以脚本应该是这样的:
<script type="text/javascript">
$(function () {
$(".inline.{{ doctor_form.prefix }}").formset({
prefix: "{{ doctor_form.prefix }}",
formCssClass: 'dynamic-formset1'
});
$(".inline.{{ photo_form.prefix }}").formset({
prefix: "{{ photo_form.prefix }}",
formCssClass: 'dynamic-formset2'
})
})
</script>