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())
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
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
Am 12.04.2012 15:25, schrieb Jani Tiainen:
> 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:
> ...
Yes, I know this. But I work in a team where not everybody is a django expert. I search a way to test for queries which
happen at import time.
.... django debug toolbar does some tricky wrapping of cursor.execute(). I guess this could be a solution.
Thomas
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())
thank you, django-devserver seems to have a lot of other nice debugging features.
Thomas