DisneylandSC
DisneylandSC

Reputation: 966

retain all data when editing using a django formset

Suppose I have a model FooBar containing two fields Foo and Bar. Then if I use a modelform to edit just the Foo field for existing records, I can retain the Bar data by using instance, i.e.

foobar = FooBar.objects.get(...)
foobar_form = FooBarForm(request.post, instance=foobar)

What is the equivalent of this for formsets? So far I have tried Instance, which Django tells me doesn't exist for formsets, and initial, which I use to populate the formset in the GET request,

foobar = FooBar.objects.filter(...)
foobar_formset = FooBarFormSet(request.post, initial = foobar.values())

Excluding the initial argument makes has_changed() always return True, while including the initial argument makes has_changed() reflect the actual state of the form POST data. This suggests to me that the bar field data is picked up somewhere, yet when I iterate over foobar_formset and do

for foobar_form in foobar_formset:
    foobar_form.save()

I get an error from the debugger saying null value in column "Bar" violates not-null constraint. DETAIL: Failing row contains ('foo_value', null).

Upvotes: 0

Views: 336

Answers (1)

Related Questions