Formsets Are The Coolest and Hardest Part of Django

I have been using Django for the last 3 years or so…and it’s amazing! However, I now feel like I’m taking a PhD test in Django Formsets and struggling miserably. I actually do have the standard inline formsets working well after about a week of development. I’m trying to build on that now and I am continuing to struggle. Specifically, I keep running into save errors after I’ve used the self.queryset to populate my formset. Has anyone else faced similar challenges or is it just me? Doing something like…

        if (form.is_valid() and company_contact_form.is_valid()):
            contacts = company_contact_form.save(commit=False)
            for contact in contacts:
                contact = form.save(commit=False)
                contact.save()
            return self.form_valid(form, company_contact_form)

Usually tells me that save was prohibited to prevent dataloss due to unsaved related object. I’ve searched high and low and using atomic and varying approaches to form.save(commit=False) are yielding nothing as well.

We might need to see the models involved, and the complete view that handles the post - but basically, if you’ve got a formset of a list of models having an FK relationship with another model, you need to save the base model first, and then assign the pk to the individual instances of the related models.

Also, when you’re posting code, please enclose it between lines of three backticks - `. This means you’ll have a line of ```, then your code, then another line of ```.

Thanks for the response. I documented my issue pretty thoroughly on Stack Overflow. I’m new here so I don’t know if I’m allowed to post it here too? django - How do I get FormSet to validate in my view? - Stack Overflow

Thanks in advance for any thoughts.

Yes, you’re more likely to get a response from the people here if you post it here.

Thanks for the feedback. Should I post it somewhere else on here or how I did it should suffice?

Nope, posting your question here in this thread is fine.

Thought I’d give a go at trying to replicate your situation. Looks like the CreateTeamForm is not in your post though, could you maybe add that as well?

In your SO post you say you can’t use a ForeignKey and looks like you maybe changed it to IntegerField in the post - is that a requirement? (The parameters to the IntegerField constructor still seem to be remnants from using a ForeignKey; eg. on_delete=models.CASCADE etc).

Thanks! I’ll add it now. Yes…I can’t use ForeignKey and I have to use an integer field that I am setting myself. The IntegerField constructor…yeah a typo on my part. The CreateTeamForm is a standard modelform…

I figured it out. I was overriding Post unnecessarily among several other things. Long day. Thanks for attempting it but wanted to let you know that mystery has been solved. The last piece of the puzzle for me is that now I’m getting errors for my new model and form telling me that my formset data that I’m using as input via a queryset is not valid because the inline value did not match the parent instance. I’m aware that Django is detecting that this data isn’t associated with this model. I have to believe that I can use the data from one table in another. The way I am getting the data is via the INIT of the formset. Is there some other way I should be getting it that wouldn’t throw this error? Like a get_initial? I documented the same issue here on SO. django - How do I use formset data as initial data in my forms? Error is ' The inline value did not match the parent instance.' - Stack Overflow
Thanks for your time.