Trying to pass a QuerySet as initial data to a formset
Asked Answered
T

2

19

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?

Tenia answered 17/7, 2011 at 0:2 Comment(0)
H
15

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

Higgle answered 22/12, 2012 at 17:38 Comment(1)
The OP uses formset_factory, while queryset is only possible with modelformset_factory. At least, in some Django version, your code will not workShawn
B
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

Barmecide answered 17/7, 2011 at 1:44 Comment(2)
That did it. I was unaware of the values() function, so thanks so much for the heads-up. Back to work...Tenia
Thanks a lot. You misspelled the function in the example though. It should say list_of_active_products = Product.objects.filter(status=1).values()Miltonmilty
H
15

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

Higgle answered 22/12, 2012 at 17:38 Comment(1)
The OP uses formset_factory, while queryset is only possible with modelformset_factory. At least, in some Django version, your code will not workShawn

© 2022 - 2024 — McMap. All rights reserved.