通过模板中的Django ModelForm实例进行迭代

时间:2015-03-17 20:45:00

标签: python django templates views modelform

我正在使用模板标签迭代模板中的查询集,以显示现有数据库条目(即客户订单)的数据。但是,我希望允许用户为每个客户订单编辑其中一个字段(即交货备注)。

我的方法是使用ModelForm,但是当我遍历模板中的客户订单时,我无法迭代每个表单的实例。

我试图将一个实例迭代到一个ModelForm,但是我被卡住了,因为我无法在上下文中(在views.py中)将实例传递给模板中的ModelForm,它是传递给它的整个查询集。模板,而不是实例。也许我正在以错误的方式解决这个问题。

我的代码如下,感谢您提供的任何帮助:

Models.py

from django.db import models
from products.models import Product
from counters.models import Counter
from promo.models import Promo
from django.contrib.auth.models import User


class Order(models.Model):
    order_status = models.ForeignKey('OrderStatus')
    products = models.ManyToManyField(Product, through='OrderProductDetails', through_fields=('order','product'), null=True, blank=True)
    counter = models.ForeignKey(Counter, null=True, blank=True)

    order_type = models.ForeignKey('OrderType')
    order_remarks = models.CharField(max_length=1000, null=True, blank=True)
    order_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    ordered_by = models.ForeignKey(User, null=True, blank=True)

    promo = models.ForeignKey('promo.Promo', verbose_name="Order for which Promotion (if applicable)", null=True, blank=True)

    delivery_date = models.DateField(blank=True, null=True)
    delivery_remarks = models.CharField(max_length=1000, null=True, blank=True)

    updated_on = models.DateTimeField(auto_now_add=False, auto_now=True)

    class Meta:
        verbose_name = "Order"
        verbose_name_plural = "*Orders*"

    def __unicode__(self):
        return str(self.id)


class OrderProductDetails(models.Model):
    order = models.ForeignKey('Order')
    product = models.ForeignKey('products.Product')
    quantity = models.PositiveIntegerField()
    selling_price = models.DecimalField(decimal_places=2, max_digits=10)
    order_product_remarks = models.ForeignKey('OrderProductRemarks',blank=True, null=True)

    class Meta:
        verbose_name_plural = "Order - Product Details"
        verbose_name = "Order - Product Details"

    def __unicode__(self):
        return str(self.id)


class OrderProductRemarks(models.Model):
    order_product_remarks = models.CharField(max_length=240, null=False, blank=False)

    class Meta:
        verbose_name_plural = "Order Product Remarks"

    def __unicode__(self):
        return str(self.order_product_remarks)


class OrderStatus(models.Model):
    order_status_number = models.PositiveIntegerField(null=False, blank=False)
    order_status = models.CharField(max_length=100, null=False, blank=False)

    class Meta:
        verbose_name_plural = "Order Status"

    def __unicode__(self):
        return str(self.order_status_number) + ". " + str(self.order_status)


class OrderType(models.Model):
    order_type = models.CharField(max_length=100, null=False, blank=False)

    class Meta:
        verbose_name_plural = "Order Type"

    def __unicode__(self):
        return str(self.order_type)

Views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.admin.views.decorators import staff_member_required
from orders.models import Order
from orders.forms import OrderForm, RemarksForm
from products.models import Product

@login_required(login_url='/admin/login/?next=/')
def warehouseOrders(request):

    queryset = Order.objects.filter(order_status__order_status_number = 2) #Filter through default queryset manager with filter through FK

    form = RemarksForm(request.POST or None)

    if form.is_valid():
        form.save()

    context = {'queryset': queryset, 'form': form}
    template = 'warehouse_orders.html'
    return render(request, template, context)

Forms.py

from django import forms
from .models import Order

class RemarksForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ['delivery_remarks']

Template.html

{% extends 'base_frontend.html' %}
{% load crispy_forms_tags %}

{% block head_title %}
({{ queryset|length}}) Warehouse Orders
{% endblock %}

{% block head_styles %}
{% endblock %}

{% block jquery %}
{% endblock %}


{% block content %}
    <h1>Orders to Pack</h1>
<br>

{% for item in queryset %}

Order ID: {{ item }}<br>
<b>Order Status: {{ item.order_status }}</b><br>
Counter: {{ item.counter }}<br>
Order Type: {{ item.order_type }}<br>
Order Remarks: {{ item.order_remarks }}<br>
Order Date: {{ item.order_date }}<br>
Sales Rep: {{ item.ordered_by }}<br>
Promo: {{ item.promo }}<br>
Delivery Date: {{ item.delivery_date }}<br>

<table class="table table-striped table-bordered">
    <tr>
        <th class="bottom-align-th">#</th>
        <th class="bottom-align-th">Article No.</th>
        <th class="bottom-align-th">Barcode No.</th>
        <th class="bottom-align-th">Color</th>
        <th class="bottom-align-th">Free Size</th>
        <th class="bottom-align-th">3MTH<br>110<br>S</th>
        <th class="bottom-align-th">6MTH<br>120<br>M</th>
        <th class="bottom-align-th">9MTH<br>130<br>L</th>
        <th class="bottom-align-th">------<br>140<br>XL</th>
        <th class="bottom-align-th">------<br>150<br>XXL</th>
        <th class="bottom-align-th">Unit Price</th>
        <th class="bottom-align-th">Total Quantity</th>
        <th class="bottom-align-th">Remarks</th>
    </tr>
    {% for product in item.products.all %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ product.article_number }}</td>
        <td>{{ product.barcode }}</td>
        <td>{{ product.color }}</td>
        <td>{{ product.quantity }}</td>
    </tr>
    {% endfor %}
</table>
<br>

Delivery Remarks: {{ item.delivery_remarks }}<br>

    {% if form %}
        <form method="POST" action=""> {% csrf_token %}
            {{ form|crispy }}
            <input type="submit" value="Save" class="btn btn-default"/>
        </form>
    {% endif %}
<br>
<button class="btn btn-success btn-lg">Start Packing</button>
<button class="btn btn-primary btn-lg">Finish Packing</button>
<button class="btn btn-danger btn-lg">Send Order to HQ for Changes</button>
{% endfor %}


{% endblock %}

顺便说一句,在这里使用Django 1.7.2。

0 个答案:

没有答案