0
我想使用Django inlineformset_factory與自定義表單並想要使用django-dynamic-formset添加或刪除該inlineformset。如何使用Django inlineformset_factory與自定義表單
形式:
class CreatePacketForm(forms.ModelForm):
"""
CreatePacketForm class
"""
class Meta:
model = Packet
exclude = ('customer', 'created_on', 'updated_on',
'created_by', 'updated_by', 'remark', 'p_id'
)
ItemFormSet = inlineformset_factory(Packet, Item, form=CreatePacketForm,
formset=CreateItemForm,
extra=1, max_num=20, can_delete=False)
查看:
class CreatePacketView(LoginRequiredMixin, CreateView):
"""
"""
model = Packet
form_class = CreatePacketForm
template_name = CREATE_PACKET_TEMPLATE
def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
item_form = ItemFormSet()
return self.render_to_response(self.get_context_data(form=form,
item_form=item_form,))
模板:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Popup</title>
</head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br>
<div align="middle">
<span style="font-size: x-large;">Please Enter Item Details</span>
</div>
<br>
<form id="packet_form" method="post" action="" style="margin: 0 20% 0 20%;padding: 10px;">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form }}
{{ item_form.management_form }}
{{ item_form.non_form_errors }}
{% for subform in item_form %}
<div class="inline {{ item_form.prefix }}">
{{ subform }}
</div>
{% endfor %}
<div align="middle">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</div>
</form>
</div>
<script type="text/javascript" src="{% static 'girvi/js/jquery-1.3.2.min.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/jquery.formset.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/RelatedObjectLookups.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/swfobject.js' %}"></script>
<script type="text/javascript">
$(".inline.{{ item_form.prefix }}").formset({
prefix: "{{ item_form.prefix }}"
});
</script>
</body>
</html>
我inlineformset而不自定義表單集工作的罰款。但是,當我在我的模板中呈現它時,它不具有management_form
以及我的formset中的所有字段都可以被刪除。
沒有定製渲染模板表單集
<!DOCTYPE html>
<html>
<head></head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br></br>
<div align="center"></div>
<br></br>
<form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post">
<input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"></input>
<input id="id_item_set-TOTAL_FORMS" type="hidden" value="1" name="item_set-TOTAL_FORMS"></input>
<input id="id_item_set-INITIAL_FORMS" type="hidden" value="0" name="item_set-INITIAL_FORMS"></input>
<input id="id_item_set-MIN_NUM_FORMS" type="hidden" value="0" name="item_set-MIN_NUM_FORMS"></input>
<input id="id_item_set-MAX_NUM_FORMS" type="hidden" value="20" name="item_set-MAX_NUM_FORMS"></input>
<input id="id_item_set-0-id" type="hidden" name="item_set-0-id"></input>
<div class="inline item_set dynamic-form"></div>
<a class="add-row" href="javascript:void(0)"></a>
<div align="center"></div>
</form>
</div>
<script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/static/girvi/js/jquery.formset.js" type="text/javascript"></script>
<script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"></script>
<script src="/static/girvi/js/swfobject.js" type="text/javascript"></script>
<script type="text/javascript"></script>
</body>
</html>
和模板的自定義表單集
<!DOCTYPE html>
<html>
<head></head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br></br>
<div align="center"></div>
<br></br>
<form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post">
<input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"></input>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<a class="add-row" href="javascript:void(0)"></a>
<div align="center"></div>
</form>
</div>
<script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/static/girvi/js/jquery.formset.js" type="text/javascript"></script>
<script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"></script>
<script src="/static/girvi/js/swfobject.js" type="text/javascript"></script>
<script type="text/javascript"></script>
</body>
</html>
你能告訴我,那又有什麼用'kwarg form'的? – 2014-11-01 18:16:17
https://docs.djangoproject.com/en/dev/ref/forms/models/#django.forms.models.inlineformset_factory如果你的意思是'formset',那麼你可以覆蓋它的複雜用途,但'BaseModelFormSet' https ://docs.djangoproject.com/en/dev/topics/forms/modelforms/#django.forms.models.BaseInlineFormSet幾乎適用於所有情況(我沒有遇到過這種情況,我應該重寫它)。如果你想了解更多使用細節,請查看源代碼https://github.com/django/django/blob/master/django/forms/models.py#L550 – madzohan 2014-11-01 18:43:27