Hi there,

I have a pretty weird problem: I have a model class MyUser derived
from djang.contrib.auth.models.User. It doesn't have any additional
fields, but a custom manager, with create_user method that puts the
created object in the group MyGroup after creating it.

When calling MyUser.objects.create_user from the Python prompt it
works fine and the newly created user is a member of the group.
However, when run in a unittest, the user is not put into the group
and the assertion about the number of his groups fails.

To me it really looks as if the manager refuses to create the m2m
relation in the unittest.

Any hints would be appreciated
Cheers
    - David

class MyUser(User):
        objects = GroupEnsuringUserManager('MyGroup')

class GroupEnsuringUserManager(UserManager):
        def __init__(self, gruppenname):
                super(GroupEnsuringUserManager, self).__init__()
                self.group, created = 
Group.objects.get_or_create(name=gruppenname)

        def create_user(self, username, email, password=None):

                now = datetime.datetime.now()

                user = self.model(username=username, email=email, 
is_staff=False,
                                                 is_active=True, 
is_superuser=False, last_login=now,
                                                 date_joined=now)

                user.save()
                assert len(user.groups.all()) == 0
                user.groups.add(self.group)
                assert len(user.groups.all()) == 1
                return user

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
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