【问题标题】:how can I update multiple record list from a form in Django 1.9如何从 Django 1.9 中的表单更新多个记录列表
【发布时间】:2016-10-03 09:45:24
【问题描述】:

我有一个 shipping_details 表格,其中包含有关运输的一些信息以及该运输顺序中的所有项目。有两个模型信息,一个是 Shipment,另一个是 Items 模型。我想使用复选框更新所有 Marco Item Shipped 值(请参阅表单视图图片)。 这是我的表单视图 http://imgur.com/a/dcNZE 这是我的 forms.py,其中我将复选框链接到 Items is_shipped 字段,该值使用{{form_status.as_p}} 显示在视图中。 forms.py

class ShipmentStatus(forms.CheckboxInput):
    input_type = 'checkbox'

class ShipmentStatusForm((forms.ModelForm)):
    class Meta:
        model = Items
        fields = ['is_shipped']
        widgets = {
            'is_shipped': ShipmentStatus(),
        }

这是我的视图模型 shipment_detail.html

{% extends "_dashboardlayout.html" %}

{% block content %}

<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h2 class="page-header">Shipment Details</h2>
        </div>
        <div class="col-md-12">
            <form method="POST" action="">
                {% csrf_token %}
                {{ form.as_p }}
                <table class="table table-striped">
                    <tr>
                        <th>Item No</th>
                        <th>SKU</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Marco Item</th>
                        <th>Marco Item Shipped</th>
                    </tr>
                    {% for item in items_queryset %}
                    <tr>
                        <td>{{ item.item_no }}</td>
                        <td>{{ item.sku }}</td>
                        <td>{{ item.requested_quantity }}</td>
                        <td>{{ item.price }}</td>
                        <td>{{ item.is_send }}</td>
                        <td>{{ form_status.as_p }}</td>
                    </tr>
                    {% endfor %}
                </table>
                <input type="submit" value="Save">
            </form>

        </div>
    </div>
    <!-- /.row -->
</div>

{% endblock %}

这是我的控制py文件

def shipment_detail(request, order_id=None):
    order_queryset = Order.objects.get(order_id=order_id)
    customer_queryset = Customer.objects.all()
    address_queryset = Address.objects.all()
    items_queryset = Items.objects.filter(order_id=order_id).order_by('item_no')
    shipment_queryset = Shipment.objects.filter(order_id=order_id)
    # if there is no shipment data then generate shipment details for the Order
    if not shipment_queryset:
        form = ShipmentForm(request.POST or None)
        form_status = ShipmentStatusForm(request.POST or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.order_id = order_queryset
            order_queryset.save()
            instance.save()

        # save item status
        if form_status.is_valid():
            instance = form_status.save(commit=False)
            instance.save()

        context = {
            "form": form,
            "form_status": form_status,
            "order_queryset": order_queryset,
            "customer_queryset": customer_queryset,
            "address_queryset": address_queryset,
            "items_queryset": items_queryset,
        }
        return render(request, "shipment_detail.html", context)
    # if there is already data then updated the shipment details for the Order
    else:
        instance = get_object_or_404(Shipment, order_id=order_id)
        form = ShipmentForm(request.POST or None, instance=instance)

        if form.is_valid():
            instance = form.save(commit=False)
            instance.order_id = order_queryset
            # updated order is_shipped field according to ship_status
            if instance.status == True:
                order_queryset.is_shipped = True

            if instance.status == False:
                order_queryset.is_shipped = False

            # updated order is_completed field according to shipment is_complete field
            if instance.is_complete == True:
                order_queryset.is_completed = True

            if instance.is_complete == False:
                order_queryset.is_completed = False

            order_queryset.save()

            instance.save()

        print "form  status"

        # updated item is_shipped field
        instance_status = get_list_or_404(Items, order_id=order_id)

        for instance in instance_status:
            form_status = ShipmentStatusForm(request.POST, instance=instance)
            if form_status.is_valid():
                instance = form_status.save(commit=False)
                instance.save()

        context = {
            "form": form,
            "instance": instance,
            "form_status": form_status,
            "order_queryset": order_queryset,
            "customer_queryset": customer_queryset,
            "address_queryset": address_queryset,
            "items_queryset": items_queryset,
        }
        return render(request, "shipment_detail.html", context)

这里的问题是,当单击所有 Marco Item Shipped true 或 false 时,它​​的保存值正确,但如果我单击一个 false 另一个 true,则它不会保存值。

【问题讨论】:

    标签: django python-2.7


    【解决方案1】:

    一种方法是使用对象的pk作为复选框的值

    <input type="checkbox" value='{{item.id}}' 
                             name='for_action' id='for_action' >
    

    ,您可以使用request.POST.getlist('for_action')在您的视图中获取这些pk的列表

    给你!!

    HTML:

    <div class="col-lg-12">
            <h2 class="page-header">Shipment Details</h2>
        </div>
        <div class="col-md-12">
            <form method="POST" action="">
                {% csrf_token %}
                {{ form.as_p }}
                <table class="table table-striped">
                    <tr>
                        <th>Item No</th>
                        <th>SKU</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Marco Item</th>
                        <th>Marco Item Shipped</th>
                    </tr>
                    {% for item in items_queryset %}
                    <tr>
                        <td>{{ item.item_no }}</td>
                        <td>{{ item.sku }}</td>
                        <td>{{ item.requested_quantity }}</td>
                        <td>{{ item.price }}</td>
                        <td>{{ item.is_send }}</td>
                        <td><input type="checkbox" value='{{item.id}}' 
                         name='for_action' id='for_action' ></td>
                    </tr>
                    {% endfor %}
                </table>
                <input type="submit" value="Save">
            </form>
    
        </div>
    </div>
    <!-- /.row -->
    

    {% 端块 %}

    views.py

    def shipment_detail(request, order_id=None):
            #########
    
            # save item status
            list_of_id_for_action = request.POST.getlist('for_action')
            list_of_obj = Items.objects.filter(pk__in=list_of_id_for_action)
            list_of_obj.update(is_shipped=True)
    
            ######
    
    
            context = {
                "form": form,
                "instance": instance,
                "form_status": form_status,
                "order_queryset": order_queryset,
                "customer_queryset": customer_queryset,
                "address_queryset": address_queryset,
                "items_queryset": items_queryset,
            }
    

    希望对你有帮助

    【讨论】:

    • 这里需要打电话form.is_valid()吗?
    • 如果您使用表单并需要验证数据,可以。
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多