[Answer]-How to update Django modelform without inserting?

0👍

I was able to get the inline model formset working as per Daniel’s suggestion with help from this blog post: http://charlesleifer.com/blog/djangos-inlineformsetfactory-and-you/

Just as an FYI, one crucial point I was missing is that the formset’s instance refers to the parent model. Also, if you’re displaying the formset manually in your template, don’t forget to include {{ formset.id }}.

1👍

For some strange reason, you’re correctly passing instance to the main forms, but initial to EventScheduleModelFormSet, which is the immediate cause of the problem.

However, since these models obviously have a foreign key relationship, it sounds like you should be using a inline model formset:

from django.forms.models import inlineformset_factory
eventschedule_formset_class = inlineformset_factory(Event, EventSchedule)
event_sched_formset = eventschedule_formset_class(request.POST or None,
                                                  instance=event_to_edit)

Now there is no need for that updateEvent logic – you can just save event_sched_formset and it will correctly reference the event.

0👍

you are trying to save the EventScheduleModelFormSet form? there is no instance set only initial data so you are creating a new object every time. or am I missing something?

👤Pascal

Leave a comment