16

ユーザーが受け取ったアイテムの数量を更新できる在庫システムのページを作成しようとしています。

すべての製品の表を表示し、受け取った数量をユーザーに入力させたいのですが、これを投稿して繰り返し、データベースを更新します。

これが私の見解です:

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'がありません

誰かがこれで私を助けることができますか?

4

2 に答える 2

17

ドキュメントから、QuerySetではなく、初期データとして辞書のリストを渡す必要があるようです。

Also note that we are passing in a list of dictionaries as the initial data.

最初のクエリを次のように変更することをお勧めします。

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

于 2011-07-17T01:44:24.693 に答える
13

queryset引数を使用することもできます。これは機能するはずです:

formset = ShipmentFormSet(queryset = list_of_active_products)

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

于 2012-12-22T17:38:52.370 に答える