I have an inline formset factory with a Parent entity and a child entiy.
I need to set the child form initial values depending on the parent's attributes.
I thought that doing this in the init
of my BaseFormset
form would be the best way.
So here's what I do:
class MyBaseFormset(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
#Crispy
...
super(MyBaseFormset, self).__init__(*args, **kwargs)
form = self.forms[0]
temp = Child()
temp.x = "a"
temp.y = "z"
form.instance = temp
The first time I executed this it seems to work - but now I always get an empty/default form for my child.
Any ideas what I might be doing wrong? I don't get any errors...
Sometimes you don't see the wood because of all the trees...
Just use inital value and not an object:
form.fields['x'].initial = self.instance.x