I am fairly new to Django. I am trying to set the groups
field for User
using factory_boy
. The default User
class has a field _groups
. I tried setting that, but that is not helping.
class GroupFactory(factory.django.DjangoModelFactory):
class Meta:
model = Group
name = Sequence(lambda n: "group_{0}".format(n))
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
username = factory.Sequence(lambda n: "user_{0}".format(n))
password = "test"
first_name = u'ßhamra'
last_name = u'ßhamra'
_groups = factory.SubFactory(GroupFactory)
@classmethod
def _create(cls, model_class, *args, **kwargs):
"""
Override the default ``_create`` with our custom call.
Due to internal behavior or create user create method that forces is_staff kwarg
"""
g = GroupFactory("abc")
manager = cls._get_manager(model_class)
is_staff = kwargs.pop('is_staff', None)
user = manager.create_user(*args, **kwargs)
if is_staff:
user.is_staff = is_staff
user.save()
return user
groups
field explicitly inUsers
Model. I am getting the error'groups' is an invalid keyword argument for this function
. I have been stuck in this for very long now. – Muskeg