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

On Thursday, November 21, 2013 9:25:56 AM UTC+2, schinckel wrote:
>
> 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).
>
>     
> https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#custom-database-types
>
> "The 
> db_type()<https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.Field.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:
>
>     
> https://github.com/django/django/blob/master/django/db/models/sql/where.py#L368
>
> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/0338c0f8-941a-4736-998d-f47d55261ad3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to