OK, more insight.

This appears to be a figment of the testing framework.

I didn't mention that I also had a file tests.py and the duplicate
problem is generated when the model that holds the doctests is
imported into tests.py.  (I agree that the test code shown below
shouldn't sit in a model doctest)

So, to recreate this behavior, create a models.py that looks like
this:

def MyModel(models.Model):
    """
    >>> u=User.objects.create_user('test2','[EMAIL PROTECTED]')
    >>> u.email='new'
    >>> u.save()
    """
    anyfield = models.BooleanField()

COUNT = 0

def user_pre_save(sender, instance, signal, *args, **kwargs):
    global COUNT
    r = random.randint(1,50)
    print 'pre_save: count: %s random: %s' %(COUNT, r)
    COUNT +=1

dispatcher.connect(user_pre_save, signal=signals.pre_save,
sender=User)

Then create a tests.py and add this import (that's all you need):

from myproject.models import MyModel


You'll get the behavior I displayed earlier.  I'll see if I can figure
out what is causing this.

Dave

On Jul 9, 10:15 am, davenaff <[EMAIL PROTECTED]> wrote:
> This behavior is really quite bizarre and I've spent a tremendous
> amount of time trying to figure it out.
>
> Here is the code to demonstrate the problem: In one of my models.py
> files, I have this:
>
> COUNT = 0
>
> def user_pre_save(sender, instance, signal, *args, **kwargs):
>         global COUNT
>         r = random.randint(1,50)
>         print 'pre_save: count: %s random: %s' %(COUNT, r)
>         COUNT +=1
>
> dispatcher.connect(user_pre_save, signal=signals.pre_save,
> sender=User)
>
> I have a simple doctest:
>         >>> u=User.objects.create_user('test2','[EMAIL PROTECTED]')
>         >>> u.email='new'
>         >>> u.save()
>
> I load one user via the initial data fixtures.
>
> Here is the output I get:
>
> Installing json fixture 'initial_data' from 'c:\django\bv\..\bv\alerts
> \fixtures'
> .
> pre_save: count: 0 random: 49
> pre_save: count: 0 random: 23
> ...
> Failed example:
>     u=User.objects.create_user('test2','[EMAIL PROTECTED]')
> Expected nothing
> Got:
>     pre_save: count: 1 random: 13
>     pre_save: count: 1 random: 2
> ...
>
> Failed example:
>     u.save()
> Expected nothing
> Got:
>     pre_save: count: 2 random: 18
>     pre_save: count: 2 random: 49
>
> If I take the same example and execute it in the shell, it behaves as
> expected.
>
> Thoughts, suggestions?
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to