如何在自定义formset中使用Django inlineformset_factory

时间:2014-11-01 15:51:43

标签: javascript django django-forms django-views

我想将Django inlineformset_factory与自定义formset一起使用,并希望使用django-dynamic-formset添加或删除该inlineformset。

形式:

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工作正常,没有自定义formset。但是当我在我的模板中呈现它时,它没有management_form以及我的formset中的所有字段都可以删除。

没有自定义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>

带有自定义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>
                <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>

without formset enter image description here

1 个答案:

答案 0 :(得分:1)

中的问题
ItemFormSet = inlineformset_factory(Packet, Item, form=CreatePacketForm,
                                formset=CreateItemForm,
                                extra=1, max_num=20, can_delete=False)

form kwarg这里是自定义 formset表单

所以它应该是:

ItemFormSet = inlineformset_factory(Packet, Item, form=CreateItemForm, extra=1, max_num=20, can_delete=False)