On Thu, Nov 26, 2009 at 6:01 AM, Tom Evans <tevans...@googlemail.com> wrote: > On Thu, Nov 26, 2009 at 2:35 AM, fallhunter <fallhunt...@gmail.com> wrote: >> >> 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? >> > > This happens if you import the same file using two different names. Eg, here > you are importing it as 'from models import *'. In some other part of your > code, you probably have 'from foo.models import *'. Python treats them as > two different files, and hence the signal is attached twice.
BTW, this is not true for regular Python imports: $ cat > mytest.py print "hello" $ python >>> import mytest hello >>> from mytest import * >>> >>> import mytest as yourtest >>> It may be loaded twice (or more) by different threads or by another import mechanism (which seems to be the case here): http://code.djangoproject.com/wiki/Signals See section on "Help, post_save seems to be emitted twice for each save!" -Doug > Remove/rework the spurious import, and everything will work. > > Cheers > > Tom > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@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. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.