我对 Django 很陌生。我正在尝试设置使用groups
字段。默认类有一个字段。我尝试设置它,但这没有帮助。User
factory_boy
User
_groups
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