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

baker.make only creates value for username on Django's User model #74

Closed
mazulo opened this issue Jun 2, 2020 · 4 comments
Closed

baker.make only creates value for username on Django's User model #74

mazulo opened this issue Jun 2, 2020 · 4 comments

Comments

@mazulo
Copy link

mazulo commented Jun 2, 2020

Short summary.
I'm not sure if I'm missing something, and please let me know if I am, but whenever I create a user with baker.make it creates a user but only username has value in it. I'd expect .make to generate values for the other fields it supports, like EmailField.

Expected behavior

baker.make(User) would create random values for all fields it supports, including EmailField.

Actual behavior

It doesn't create an email value for the user.

Reproduction Steps

How to reproduce this issue.

In [1]: from model_bakery import baker                                                                                                                                                                                                                                                            

In [2]: from django.contrib.auth.models import User                                                                                                                                                                                                                                               

In [3]: user = baker.make(User)                                                                                                                                                                                                                                                                   

In [4]: user.email                                                                                                                                                                                                                                                                                
Out[4]: ''

In [5]: user.username                                                                                                                                                                                                                                                                             
Out[5]: 'tGIavQwgHZslfbKYWZlhJQOmo [truncated]'

In [6]: user.first_name                                                                                                                                                                                                                                                                           
Out[6]: ''

In [7]: user.last_name                                                                                                                                                                                                                                                                            
Out[7]: ''

Versions

Python: 3.6.9
Django: 1.11.29
Model Bakery: 1.1.0

Also tested on

Versions

Python: 3.8.3
Django: 3.0.6
Model Bakery: 1.1.0

@amureki
Copy link
Collaborator

amureki commented Jun 13, 2020

Greetings @mazulo !
Thanks for posting the issue here and sorry for the late reply!

By default, model_bakery will skip fields with null=True or blank=True set. And if we are talking about django.contrib.auth.models.User - email field is defined with blank=True there.

However, I have a couple of suggestions here for you:

  • you can explicitly generate email during object generation (if you need this behaviour in a certain place):
from model_bakery.random_gen import gen_email
user = baker.make(User, email=gen_email)

print(user.email)
# 'lcwAseTdRA@example.com'
  • use recipes and define user recipe that generates this email (if you want to have a commonly used logic for multiple tests):

# myapp/baker_recipes.py
from model_bakery.random_gen import gen_email
from model_bakery.recipe import Recipe

user_recipe = Recipe(
    User,
    email=gen_email
)

user = baker.make_recipe('myapp.user_recipe')
print(user.email)
# 'VAFvykvQhY@example.com'

Please, tell me if something is unclear and/or confusing!

Best,
Rust

P.S. Not related to the library, but I would highly recommend not to use User model from django contrib directly:
https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#referencing-the-user-model

@berinhard
Copy link
Member

Hey @mazulo thanks for opening this and @amureki for helping us out =)

But, actually, there's an even better approach to populate non-required fields with the _fill_optional parameter. You have 2 possible ways to use it:

user = baker.make(User, _fill_optional=True)

## OR

user = baker.make(User, _fill_optional=['email'])

The first one will populate all optional fields, meanwhile the second only the email one.

I now realized that we don't cover the _fill_optional kwarg in our documentation. So, it'd be nice to have it documented somewhere =)

@berinhard
Copy link
Member

I've specifically addressed the docs issue on #79, so I'm closing this one, ok?

@mazulo
Copy link
Author

mazulo commented Jun 16, 2020

That makes a lot of sense. Thank you folks @amureki and @berinhard for the explanation and the hard work on this awesome lib!

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

3 participants