尝试将查询集作为初始数据传递给表单集

发布于 2024-11-24 00:30:31 字数 947 浏览 1 评论 0原文

我正在尝试为库存系统构建一个页面,该页面将允许用户更新收到的商品数量。

我想显示所有产品的表格,并让用户输入收到的数量,我将发布并迭代以更新数据库。

这是我的观点:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2024-12-01 00:30:31

从文档来看,您似乎必须传入一个字典列表作为初始数据,而不是一个查询集:

另请注意,我们正在传入一个字典列表作为初始数据。

您可以想要将初始查询更改为:

list_of_active_products = Product.objects.filter(status=1).values()

它将返回字典列表而不是模型实例对象。

将初始数据与表单集一起使用:
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:

list_of_active_products = Product.objects.filter(status=1).values()

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

以歌曲疗慰 2024-12-01 00:30:31

您还可以使用查询集参数。这应该有效:

formset = ShipmentFormSet(queryset=list_of_active_products)

cf. https://docs.djangoproject.com/en/ dev/topics/forms/modelforms/#change-the-queryset

You can also use the queryset argument. This should work:

formset = ShipmentFormSet(queryset=list_of_active_products)

cf. https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文