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

Passing kwargs to related models #55

Open
sobolevn opened this issue Dec 10, 2019 · 3 comments
Open

Passing kwargs to related models #55

sobolevn opened this issue Dec 10, 2019 · 3 comments

Comments

@sobolevn
Copy link
Contributor

Imagine that we have a model like so:

class User(models.Model):
     profile = models.ForeignKey('UserProfile') 
     is_active = models.BooleanField()

And later I want to use it like so:

active_user_factory = fakery.blueprint(User).fields(
    is_active=True,
)

empty_username = active_user_factory.m(profile__username='')
email_username = active_user_factory.m(profile__username='[email protected]')

But, currently this does not work. What can be done to pass kwargs to related objects?

@fcurella
Copy link
Owner

Hi @sobolevn

I'm not entirely sure a FK from User to Profile is what you want in the first place. It means that potentially more than one User can point to the same Profile instance.

What you probably want is either a OneToOneField (so each User has exactly one Profile and vice-versa), or a FK from Profile to User (ie: a User can have multiple Profiles).

@sobolevn
Copy link
Contributor Author

Well, that's just an example 🙂

@fcurella
Copy link
Owner

fcurella commented Dec 10, 2019

Blueprints can use other blueprints as values for their fields.

I would try something this:

user_factory = fakery.blueprint(User).fields(is_active=True)
profile_factory = fakery.blueprint(Profile).fields(user=user_factory)

empty_username = profile_factory.m(username='')
email_username = profile_factory.m(username='[email protected]')

Keep in mind that this will create a different user on your database every time you call profile_factory, so you'll end up with 2 users.

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

No branches or pull requests

2 participants