Bug in django\forms\models.py in _construct_form, line 593?

112 views
Skip to first unread message

Piotr Gosławski

unread,
Sep 6, 2014, 7:46:41 AM9/6/14
to django-d...@googlegroups.com
Hi,

when using UpdateView with formset (inlineformset_factory), I randomly got KeyError (page works, refresh, error, refresh, works, ...)

Django Version:     1.7
Exception Type:     KeyError
Exception Value:     0
Exception Location:     ...\django\forms\models.py in _construct_form, line 593
Python Version:     3.4.0

I looked up django source and found:

    except IndexError:
       
pass

I thought it was possibly caused by some changes in python 3.x, so I just changed it to

    except (IndexError, KeyError):
       
pass

and it seems to work, but I want to make sure I'm not sweeping something else under the rug.
So... am I?

Tim Graham

unread,
Sep 6, 2014, 8:16:20 AM9/6/14
to django-d...@googlegroups.com
Hi Piotr,

What you've provided is a bit short on details for us to tell. Have you tried searching Trac for a similar issue? "Is this a bug?" questions should usually be asked on the django-users mailing list. A minimal project we can use to reproduce the error is really helpful. If you think it's a Python 3 issue, then see if you can reproduce the issue on Python 2.

Tim

Piotr Gosławski

unread,
Sep 8, 2014, 1:39:46 PM9/8/14
to django-d...@googlegroups.com
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?

Josh Smeaton

unread,
Sep 8, 2014, 6:46:58 PM9/8/14
to django-d...@googlegroups.com
At this point I think you're in the realm of django-users. This is probably a bug in user code rather than django, and you'll get better help from the django-users mailing list or IRC. At a guess the issue may be related to your views not being threadsafe, so you should show your entire view, and how you plug that view into your urlconf.

Regards,
Reply all
Reply to author
Forward
0 new messages