12.4.2012 15:09, Thomas Guettler kirjoitti:
Hi,

sometimes it happens, that db queries get executed at import time
(during importing the file by the interpreter).
That's waste of time a resources.

Is there a way to test how many queries get executing during import? I
want some automated way to detect these db queries.

Example:

def mychoices():
for obj in MyModel.objects.all(): # this hits the db during import.
That's not good.
....

class MyForm(forms.Form):
foo=forms.ChoiceField(choices=mychoices())



That is because you pass parameter as a function call, so of course function must be evaluated. So in your code reads:

Declare class attribute "foo" as a new instance of "forms.ChoiceField" with parameter of "choices" with values returned from "mychoices".

So to get rid of that part, you can pass method or a function as a parameter and it will be evaluated at the runtime:

class MyForm(forms.Form):
    foo=forms.ChoiceField(choices=mychoices)

--

Jani Tiainen


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to