Trying to pass a QuerySet as initial data to a formset

后端 未结 2 1815
天命终不由人
天命终不由人 2020-12-30 06:56

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 t

相关标签:
2条回答
  • 2020-12-30 07:19

    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

    0 讨论(0)
  • 2020-12-30 07:29

    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

    0 讨论(0)
提交回复
热议问题