Override _after_postgeneration to force save in User factory (#4534)

* Override _after_postgeneration to force save

Saving in _after_postgeneration was automatic in Factory Boy versions below 4.0, but must be done explicitly in newer versions.

* Clean up formatting
This commit is contained in:
John 2023-08-19 15:31:53 -04:00 committed by GitHub
parent 2ff9a12c30
commit 13b97de9d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,13 @@ class UserFactory(DjangoModelFactory):
)
self.set_password(password)
@classmethod
def _after_postgeneration(cls, instance, create, results=None):
"""Save again the instance if creating and at least one hook ran."""
if create and results and not cls._meta.skip_postgeneration_save:
# Some post-generation hooks ran, and may have modified us.
instance.save()
class Meta:
model = get_user_model()
django_get_or_create = ["{{cookiecutter.username_type}}"]