尝试将查询集作为初始数据传递给表单集
我正在尝试为库存系统构建一个页面,该页面将允许用户更新收到的商品数量。
我想显示所有产品的表格,并让用户输入收到的数量,我将发布并迭代以更新数据库。
这是我的观点:
def new_shipment(request):
list_of_active_products = Product.objects.filter(status=1)
ShipmentFormSet = formset_factory(ShipmentForm, extra=0)
formset = ShipmentFormSet(initial=list_of_active_products)
return render_to_response('inventory/new_shipment.html', {'formset': formset})
这是我的表单模型:
class ShipmentForm(forms.Form):
sku = forms.IntegerField()
product_name = forms.CharField(max_length=100)
quantity = forms.IntegerField()
这是表单模板:
<form method="post" action="">
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
<input type="submit" />
</form>
这是我收到的错误:
渲染时捕获 AttributeError: 'Product' 对象没有属性 'get'
任何人都可以帮助我这?
I'm trying to build a page for an inventory system that will allow a user to update a quantity of items received.
I want to show a table of all products and let the user enter the quantity received, which I'll post and iterate over to update the database.
Here is my view:
def new_shipment(request):
list_of_active_products = Product.objects.filter(status=1)
ShipmentFormSet = formset_factory(ShipmentForm, extra=0)
formset = ShipmentFormSet(initial=list_of_active_products)
return render_to_response('inventory/new_shipment.html', {'formset': formset})
Here's my model for the form:
class ShipmentForm(forms.Form):
sku = forms.IntegerField()
product_name = forms.CharField(max_length=100)
quantity = forms.IntegerField()
And here is the form template:
<form method="post" action="">
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
<input type="submit" />
</form>
And here is the error I'm getting:
Caught AttributeError while rendering: 'Product' object has no attribute 'get'
Can anyone help me out with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从文档来看,您似乎必须传入一个字典列表作为初始数据,而不是一个查询集:
另请注意,我们正在传入一个字典列表作为初始数据。
您可以想要将初始查询更改为:
它将返回字典列表而不是模型实例对象。
将初始数据与表单集一起使用:
https://docs. djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
ValuesQuerySet:
https://docs. djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values
From the docs it looks like you have to pass in a list of dictionaries as the initial data, rather than a QuerySet:
Also note that we are passing in a list of dictionaries as the initial data.
You may want to change your initial query to:
which will return a list of dictionaries rather than model-instance objects.
Using initial data with a formset:
https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
ValuesQuerySet:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values
您还可以使用查询集参数。这应该有效:
cf. https://docs.djangoproject.com/en/ dev/topics/forms/modelforms/#change-the-queryset
You can also use the queryset argument. This should work:
cf. https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset