Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mazulo/adds faker generator feature #365

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mazulo
Copy link

@mazulo mazulo commented Nov 3, 2022

Describe the change

Currently we're using some random data to feed the fields. In this PR I'm proposing an approach where the user can enable an extra feature by adding _use_faker_generator=True when using baker.make. When enabled, we will use Faker to generate the data. For now we only have a small list but it can grow as needed:

  • username
  • email
  • first_name
  • last_name
  • name
  • fullname
  • full_name
  • ip
  • ipv4
  • ipv6

I think this is a really interesting feature because it will provide the users to have a more realistic data to work on.

PR Checklist

Copy link
Collaborator

@timjklein36 timjklein36 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern with these changes is that the new faker_generator_mapping circumvents the existing generator mapping mechanism, which already allows for overriding or providing new custom generators for fields of specific types.

At the very least, the new behavior should coexist with the existing mechanism (meaning it should still use the existing generator mapping in any case that it can). Otherwise, there would be no way to override the generation of, say, EmailFields where the name of the field is in the new list.

I like the idea, but I think it deserves some extra thought/planning to make sure it can be integrated nicely with the existing features.

model_bakery/__about__.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@@ -0,0 +1,18 @@
from typing import Callable, Dict

from faker import Faker
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import (and its subsequent behavior) should be resilient to ImportError, since we don't want to require users to install faker if they are not going to use it.

@@ -660,6 +676,11 @@ def generate_value(self, field: Field, commit: bool = True) -> Any:
`attr_mapping` and `type_mapping` can be defined easily overwriting the
model.
"""
field_name = field.attname
if self._use_faker_generator and field_name in faker_generator_mapping:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment about ImportError. I think that if we fail to import faker we should output a warning message (when _use_faker_generator is True) and continue past this logic so that it does not break, but still gives the user feedback that they are trying to use an optional feature for which they do not have all the requirements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's cleaner to fail when someone tries to use the feature without installing faker.
But not using the feature should still be possible without faker being installed.

moving the from .faker_gen import faker_generator_mapping inside this if clause should do the trick...?

@mazulo
Copy link
Author

mazulo commented Nov 7, 2022

Hey @timjklein36 thank you for your review! I'm going to read and better understand about the changes you requested (mainly the one for the new feature). If you have any other idea please let me know! More than happy to go through them. 😄

@mazulo mazulo force-pushed the mazulo/adds-faker-generator-feature branch from d6ad3f3 to 77221e9 Compare November 7, 2022 12:07
@@ -660,6 +676,11 @@ def generate_value(self, field: Field, commit: bool = True) -> Any:
`attr_mapping` and `type_mapping` can be defined easily overwriting the
model.
"""
field_name = field.attname
if self._use_faker_generator and field_name in faker_generator_mapping:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's cleaner to fail when someone tries to use the feature without installing faker.
But not using the feature should still be possible without faker being installed.

moving the from .faker_gen import faker_generator_mapping inside this if clause should do the trick...?

@@ -1 +1,2 @@
django>=3.2
Faker==15.1.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be moved to extras_require["faker"]

@v3ss0n
Copy link

v3ss0n commented Jan 16, 2023

Any chance of merging this? in 2023 there are no working faker baked in fixture gens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants