gpt4 book ai didi

Django 动态表单,带有 HTML 数组的表单集

转载 作者:行者123 更新时间:2023-12-03 17:55:29 27 4
gpt4 key购买 nike

我正在尝试创建一个动态表单,该表单使用模型关系来生成表单集。

以下是该应用程序的简要概述。我有一个客户表(加入到 django 管理员用户表)、产品表、订单表和一些连接表。 customer_product 表包含一个可以编辑的预建订单。我使用一些连接表来创建新的 order_instances,可以从订单表中引用历史数据。

` 客户与用户表具有一对一的关系

customer table
--------------
id


product table
-------------
id | product_id

` 客户拥有并属于许多产品
customer_product (pre-built order templates)
----------------
id | customer_id | product_id

' 客户拥有并属于许多产品。客户有很多订单
customer_product_order (customer initiated order)
----------------------
id | customer_id | product_id | order_id | quantity

order (main order table. each record contains meta data for that order)
-------
id | invoice_number | delivery_date

我不知道在这种情况下如何使用表单集,这将让我们构建动态表单并保存到 customer_product_order 表和 order 表中。目前我有表单输出,但它第二次检查 customer_product 表,而不是让 forms.py 构造它。也无法在 forms.py 页面中构造 html 数组。

模型.py
from django.conf import settings
from django.db import models
from datetime import datetime
import pprint

class Customer(models.Model):
customer = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'groups__name': "customers"})
customer_product = models.ManyToManyField('Product', through='CustomerProduct')
company_name = models.CharField(max_length=255)
address1 = models.CharField(max_length=255)
address2 = models.CharField(max_length=255)
city = models.CharField(max_length=255)
state = models.CharField(max_length=255)
zip_code = models.CharField(max_length=255)
created_date = models.DateTimeField('date created')
def __unicode__(self):
return self.company_name

class CustomerProduct(models.Model):
customer = models.ForeignKey('Customer')
product = models.ForeignKey('Product')
def __unicode__(self):
return self.customer.company_name

class Product(models.Model):
item = models.CharField(max_length=255)
description = models.CharField(max_length=255)
def __unicode__(self):
return self.description

class Order(models.Model):
customer_product_order = models.ManyToManyField('CustomerProduct', through='CustomerProductOrder')
purchase_order_number = models.CharField(max_length=10)
person_placing_order = models.CharField(max_length=255)

class CustomerProductOrder(models.Model):
order = models.ForeignKey('Order')
customer_product = models.ForeignKey('CustomerProduct')
quantity = models.IntegerField(default=0)
instructions = models.CharField(max_length=2000)
order_correct = models.BooleanField()
def __unicode__(self):
return self.customer_product.customer.company_name

class Meta:
verbose_name = "Customer Order"

表格.py
from django import forms

from .models import CustomerProduct

class OrderForm(forms.Form):
def __init__(self, *args, **kwargs):
products = CustomerProduct.objects.filter(customer_id=1)

super(OrderForm, self).__init__(*args, **kwargs)
counter = 1
for q in products:
self.fields['product[quantity][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
self.fields['product[item][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
self.fields['product[description][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
counter += 1

purchase_order_number = forms.CharField(required=True)
person_placing_order = forms.CharField(required=True)
delivery_date_request = forms.DateField(required=True,widget=forms.DateInput(attrs={'class':'datepicker'}))
instructions = forms.CharField(required=False,widget=forms.Textarea(attrs={'rows': 5, 'cols': 100, 'class': 'form-control'}))
confirm_order = forms.BooleanField(required=True)

View .py
from django import forms

from .models import CustomerProduct

class OrderForm(forms.Form):
def __init__(self, *args, **kwargs):
products = CustomerProduct.objects.filter(customer_id=1)

super(OrderForm, self).__init__(*args, **kwargs)
counter = 1
for q in products:
self.fields['product[quantity][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
self.fields['product[item][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
self.fields['product[description][' + str(q.id) + ']' + str(counter)] = forms.CharField(required=False)
counter += 1

purchase_order_number = forms.CharField(required=True)
person_placing_order = forms.CharField(required=True)
delivery_date_request = forms.DateField(required=True,widget=forms.DateInput(attrs={'class':'datepicker'}))
instructions = forms.CharField(required=False,widget=forms.Textarea(attrs={'rows': 5, 'cols': 100, 'class': 'form-control'}))
confirm_order = forms.BooleanField(required=True)

模板
{% extends "base.html" %}

{% block orderform %}
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}

<form action="/onlineordering/" method="POST">

<form class="form-inline">

{% csrf_token %}

<div class="row">
<div class="col-md-6">

<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputName2">Date</label></div>
<div class="col-md-7">{{ datenow }}</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputName2">Customer ID:</label></div>
<div class="col-md-7">{{ username }}</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputName2">Address</label></div>
<div class="col-md-7">
1 Main Street<br />
Town<br />
State<br />
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputName2">Purchase Order Number</label></div>
<div class="col-md-7">{{ form.purchase_order_number }}</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputEmail2">Person Placing Order</label></div>
<div class="col-md-7">{{ form.person_placing_order }}</div>
</div>
</div>
<div class="form-group">
<div class="form-group">
<div class="row">
<div class="col-md-5"><label for="exampleInputEmail2">Requested Delivery Date</label></div>
<div class="col-md-7">{{ form.delivery_date_request }}</div>
</div>
</div>
</div>

</div>
<div class="col-md-6">
<p class="text-right">

</p>
</div>
</div>

<table class="table table-bordered online-ordering-table">
<tr>
<th width="10%">Quantity</th>
<th width="20%">Item</th>
<th width="70%">Description</th>
</tr>

{% for product in products %}

<tr>
<td><input name="product_quantity" /></td>
<td>{{ product.product.item }}</td>
<td>{{ product.product.description }}</td>
</tr>

{% endfor %}

</table>

<div class="form-group">
<label for="exampleInputEmail2">Special Instructions / Comments</label>
{{ form.instructions }}
</div>

<div class="form-group">
<div class="checkbox">
<label>
{{ form.confirm_order }} I concede the order above is correct and authorize Company to fulfill this order
</label>
</div>
</div>

<input class="btn" type="submit" value="Submit">
</form>

</form>
{% endblock %}

更新

关于模型的评论。我已尽我所能详细说明了上述关系。该应用程序的设计和要求如下。每位客户 hasone 用户。客户订单表由管理员预先构建并存储在 customer_product 表中。客户 拥有和属于自己产品及客户 有很多订单 .当客户下订单时,会添加新的订单记录,并填充 customer_products_order 连接表以及每种产品的数量。

customer_product 表由管理员预先填充。 customer_product_order(联接)和订单表由客户填充。

我使用以下连接从订单模型中获取客户信息。我找不到任何其他方法从订单模型递归加入客户表。我可能是错的。尽管这超出了这个问题,但我非常感谢对我的代码的任何评论!
models.ManyToManyField('CustomerProduct', through='CustomerProductOrder')

最佳答案

从我们在评论中开始的讨论和您更新的答案中,我不确定您的目的 customer_product table ?如果我最终得到它,我会更新我的答案。

(在我看来, customer_id 模型中的 customer_product 字段与 customer_id 中的 customer_product_order 字段是多余的,使得表 customer_product 实际上没有用。)

要对产品订购系统建模,您或许可以移动 customer_idorder级别(根据我对您的要求的了解,Customer 有很多订单,但是订单有 单个 客户,当前设计无法实现),给您:

product_order
----------------------
id | product_id(ForeignKey) | order_id(ForeignKey) | quantity

order
-------
id | customer_id(ForeignKey) | invoice_number | delivery_date

如果这个模型可以满足您的需要,您能否澄清一下您面临的问题是什么?

编辑:我认为数据建模有点偏离,并且它没有传达关于您想要实现的目标的正确信息(使帮助变得复杂;)。特别是:

Customer hasand belongstomany products



似乎这可能是一种误解:为什么 Customer 会属于一个产品? Customer似乎更自然将有一个多对- Order 的关系实例。还有一个 Order将有一个多对- Products 的关系,可能通过包含 quantity 的外部表连接字段(即您的 CustomerProductOrder 表)。

这会给你这个更新和简化的模型:


customer table
--------------
id | ...


product table
-------------
id | description | ...


order
-------
id | customer_id(ForeignKey) | person_placing_order | invoice_number | delivery_date


order_content (~= customer_product_order)
----------------------
id | order_id(ForeignKey) | customer_id | product_id(ForeignKey) | product_quantity



你能解释一下为什么这个更琐碎的设计不适合你的目的吗? (希望它能让我们更好地了解您的需求)

关于Django 动态表单,带有 HTML 数组的表单集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com