[Django]-Factory boy in django for non-database fields

3👍

1👍

You can customize the _create method.

You can also disable signals if the external account is created in a signal handler.

1👍

factory_boy‘s create() calls your model’s MyModel.objects.create(**kwargs) method.

When you write MyModelFactory(), this calls:

obj = MyModel(name="name01", create_external_account=False)
obj.save()
return obj

Here, you should override your model’s __init__ method to handle your custom field:

class MyModel(django.db.models.Model):
    def __init__(self, *args, **kwargs):
        self.create_external_account = kwargs.pop('create_external_account', False)
        return super(MyModel, self).__init__(*args, **kwargs)
👤Xelnor

Leave a comment