fcurella / django-fakery

🏭 An easy-to-use implementation of Creation Methods for Django, backed by Faker.
http://django-fakery.readthedocs.org/en/stable/
MIT License
115 stars 6 forks source link

Reference instance fields inside `lambda` #54

Closed sobolevn closed 4 years ago

sobolevn commented 4 years ago

Hi! Thanks a lot for building this project.

I try to solve this problem:

fakery.m(User)(
    username='test',
    email=lambda n, f, instance: '{0}@example.com'.format(instance.username)
)

But I am not sure that I am able to with django-fakery currently.

fcurella commented 4 years ago

You can use the pre_save hook to call any logic you want right before the changes are committed to the database. This includes setting fields on the instance:

fakery.m(
    User,
    pre_save=[
        lambda i: i.email = '{0}@example.com'.format(i.username)
    ],
)(username='test')
sobolevn commented 4 years ago

Awesome! Thanks.