My project is not python 2 compatibile so I've tried to create a minimal project that would reproduce the error, but failed to do so. Turned out the view throwing KeyError was not the problem, at least not by itself. I went back to the original project and started poking at it. Finally I've noticed that the problem doesn't occur until I visit the page associated with another view.
So basically view1 was breaking the worker process in such a weird way, that view2 (the one with formset) started throwing KeyError exception. All the other views were not affected. After restarting the server it worked again unil I visited view1.
Refreshing the page was only making it apear random because I had multiple worker processes. After changing number of processes to 1 it was more clear what was happening. Problem occured on Apache with mod_wsgi and uWSGI. It was also appearing on django's devserver, but stopped and I have no idea why.
I then started looking at view1 (based on CreateView) and commented out this method (just taking a long shot):
def get_initial(self):
self.initial.update({
'start_time':datetime.datetime.utcnow().replace(tzinfo=utc) + datetime.timedelta(seconds = 30),
'end_time':datetime.datetime.utcnow().replace(tzinfo=utc) + datetime.timedelta(minutes = 60),
})
return self.initial.copy()
The problem disappeared, but I don't really understand why.
Is there something wrong with the code above?
What kind of sorcery is this?