我有一个带有产品和订单的简单Django模型结构,由一个中间表链接,确定给定订单中每个产品的数量:
models.py:
class Product(models.Model):
name = models.CharField()
class Order(models.Model):
id = models.IntegerField()
products = models.ManyToManyField(Product, through='OrderProduct')
class OrderProduct(models.Model):
order=models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
我想在我的forms.py文件中设计一个表单,1)拉出数据库中每个产品的列表; 2)为用户提供调整每个订购产品数量的选项(例如,在CharField输入中),以及3)创建新的Order和OrderProduct对象,这些对象存储用户订购的每个产品数量的记录。
但是,我无法找到一种创建表单的好方法,该表单将自动从数据库中提取所有产品并添加字段以表示给定产品的订购数量。理想情况下,结果看起来像这样,但会自动编码:
forms.py:
class OrderForm(forms.Form):
product_1_quantity = forms.IntegerField()
product_2_quantity = forms.IntegerField()
product_3_quantity = forms.IntegerField()
....等,每个产品。最好的方法是什么?是否涉及' for'循环为数据库中的每个产品创建一个新字段?
答案 0 :(得分:1)
我认为Django Formsets可能对您有用。您可以根据您决定的数量反复创建相同形式的集合,因此您的产品也是如此。看看here。您可以执行以下示例中的操作。
public cancelBSS() {
ctrl.gridApi.grid.refresh();
};
您也可以使用
等数据预先填充它class ArticleForm(forms.Form):
title = forms.CharField()
from django.forms import formset_factory
ArticleFormSet = formset_factory(ArticleForm, extra=2)
希望这有帮助!
答案 1 :(得分:0)
您可以使用ModelChoiceField
显示所有产品,而无需执行for循环:
class OrderForm(forms.Form):
product = forms.ModelChoiceField(queryset=Product.objects.all()) # with queryset you can show a checkbox with the query
quantify = forms.IntegerField()
现在您的观点应该保存您的数据。例如,如果您需要“添加新”按钮,则可以使用ajax,否则为you can use formset
答案 2 :(得分:0)
从user3140312阐述以下答案 - Django的formsets提供了解决方案!
<强> models.py:强>
class Product(models.Model):
name = models.CharField()
class Order(models.Model):
id = models.IntegerField()
products = models.ManyToManyField(Product, through='OrderProduct')
class OrderProduct(models.Model):
order=models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
<强> forms.py 强>:
class ProductForm(forms.Form):
product_id = forms.IntegerField(widget=forms.HiddenInput)
quantity = forms.IntegerField()
<强> views.py 强>:
from .forms import ProductForm
from django.forms import formset_factory
from .models import Product
# Show a form asking for the user's product quantities
def product_form(request):
# If this form is being processed...
if request.method == 'POST':
ProductFormSet = formset_factory(ProductForm)
# Grab the form from the POST variable
formset = ProductFormSet(request.POST)
# If the form is valid...
if formset.is_valid():
# Do whatever you want with the results
# If the form is not being processed, create a new ProductFormSet and render it
else:
product_list = []
for product in Product.objects.all():
product_list.append({'product_id': product.id, 'quantity': 0})
ProductFormSet = formset_factory(ProductForm, extra=0)
formset = ProductFormSet(initial=product_list)
return render(request, 'template.html', {'formset': formset})
<强> template.html:强>
<form action="{% url '' %}" method="post">
{% csrf_token %}
{{ formset.as_table }}
{{ formset.management_form }}
<input type="submit" value="Submit" />
</form>