Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import twice when run test

i have this code in my tests.py:

from models import *

and in the models.py I have a signal handler and register it with

post_save.connect( post_save_note, sender=Note )

and when i run test with ./manage.py test main

I found the signal handler was registered twice and executed twice, and I found it's because the models was imported twice.

can't i put from models import * in the test code? what should i do?

like image 909
fallhunter Avatar asked Nov 26 '09 02:11

fallhunter


1 Answers

You need to make sure your models are always imported the same way.

So for example, if you have in example1/tests.py:

from models import *

and in another package example2/views.py:

from example1.models import ModelA

then you'll have the problem you are experiencing. The solution is to be consistent and use only one or the other.

like image 55
Van Gale Avatar answered Nov 09 '22 13:11

Van Gale