db_type is called more than documented.

49 views
Skip to first unread message

schinckel

unread,
Nov 21, 2013, 2:25:56 AM11/21/13
to django-d...@googlegroups.com
I maintain the django-jsonfield module, and I have, as the documentation suggests, some relatively expensive code in JSONField.db_type.

The documentation is pretty clear on this as being _the_ place to put a test like this (that checks to see if the database can handle a json field type, or if we should just store it as text).


"The db_type() method is only called by Django when the framework constructs the CREATE TABLE statements for your application – that is, when you first create your tables. It’s not called at any other time, so it can afford to execute slightly complex code, such as the connection.settings_dict check in the above example."

However, I noticed that the SQL query I was running to check for if I should use a json field was running way more than expected.

It turns out that this method is called at:


All other calls to this method appear to be in CREATE statements.

Is this something that just slipped under the radar? Is it something that should be fixed in documentation? Any suggestions?

Regards,

Matt.

Anssi Kääriäinen

unread,
Nov 21, 2013, 5:47:02 AM11/21/13
to django-d...@googlegroups.com
It seems the db_type() method has been called as part of WhereNode construction always (that is, first commit introducing it is QuerySet refactor in 2008). So, the docs are wrong. If the result is expensive to calculate, you'll need to cache it.

I created a ticket for this, see https://code.djangoproject.com/ticket/21478#ticket

 - Anssi
Reply all
Reply to author
Forward
0 new messages