我正在将djongo
用于MongoDB与Django的集成。我在模型中使用了两个ArrayModelField
,内部使用了Django的formsets
。我使用了Dynamic Formset Plugin,它可以动态添加更多ArrayModelField项,但是我不能提交空的ArrayModelField项,即给定的表单集没有表单。如何添加?
下面是我的代码:
models.py
(源和目标是将ip,端口,数据库,用户,密码作为字段的抽象模型)
class LobDetail(models.Model):
lob_name = models.CharField(max_length=64, primary_key=True)
type = models.CharField(max_length=20)
source = models.ArrayModelField(
model_container = Source,
)
destination = models.ArrayModelField(
model_container = Destination,
)
def __str__(self):
return self.lob_name + " "+ self.type
views.py
(LobDetailForm是包含所有字段的简单表单)
def add_lobdetail(request):
form = LobDetailForm
if request.method == 'POST':
print('Hey! I AM SUBMITTED')
form = LobDetailForm(request.POST)
if form.is_valid():
form.save(commit = True)
return index(request)
else:
print('Error Form Invalid')
return render(request, 'lobconf/add_lobdetail.html', {'formset': form})
add_lobdetail.html
<script type="text/javascript">
$(function() {
$('.nestedform_one').formset({
prefix: '{{ formset.source.form_set.prefix }}',
formCssClass: 'dynamic-formset1',
addText: 'Add another source',
deleteText: 'Remove'
});
$('.nestedform_two').formset({
prefix: '{{ formset.destination.form_set.prefix }}',
formCssClass: 'dynamic-formset2',
addText: 'Add another destination',
deleteText: 'Remove'
});
})
</script>
<form method="POST">
<div class="jumbotron jumbotron-fluid">
<div class="container">
{% csrf_token %}
<!-- {{ form.management_form }} -->
<div class="form-group row">
<label for="lobName" class="col-sm-2 col-form-label">Lob Name</label>
<div class="col-sm-10">
{{ formset.lob_name }}
</div>
</div>
<div class="form-group row">
<label for="typedb" class="col-sm-2 col-form-label">Type</label>
<div class="col-sm-10">
{{ formset.type }}
</div>
</div>
<fieldset class="form-group border border-primary p-2">
<legend class="mylegend col-form-label col-sm-2 w-auto">Source</legend>
<div class="row">
{{ formset.source.form_set.management_form}}
<div class="col-sm-12 nestedform_one">
{% for form in formset.source.form_set %}
<div class="space"></div>
<!-- {{ form.management_form }} -->
{{ form.ip | placeholder:"IP Address"}}
{{ form.port | placeholder:"Port Number"}}
{{ form.database | placeholder:"Database"}}
{{ form.user | placeholder:"Username"}}
{{ form.password | placeholder:"Password"}}
{% endfor %}
</div>
</div>
</fieldset>
<fieldset class="form-group border border-primary p-2">
<legend class="mylegend col-form-label col-sm-2 w-auto">Destination</legend>
<div class="row">
{{ formset.destination.form_set.management_form}}
<div class="col-sm-12 nestedform_two">
{% for form in formset.destination.form_set %}
<!-- {{ form.management_form }} -->
{{ form.ip | placeholder:"IP Address"}}
{{ form.port | placeholder:"Port Number"}}
{{ form.database | placeholder:"Database"}}
{{ form.user | placeholder:"Username"}}
{{ form.password | placeholder:"Password"}}
{% endfor %}
</div>
</div>
</fieldset>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Add lob details</button>
</div>
</div>
</form>
AddLobDetail扩展了base.html,其中添加了引导程序,jquery和动态formset jquery插件。
我是Django的新手,真的很挣扎。任何帮助表示赞赏。感谢您的时间和帮助。