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

Add field lookup syntax to _fill_optional #29

Open
berinhard opened this issue May 6, 2019 · 3 comments
Open

Add field lookup syntax to _fill_optional #29

berinhard opened this issue May 6, 2019 · 3 comments

Comments

@berinhard
Copy link
Member

I'll base my feature request using the following models as examples:

class FkModel(models.Model):
    not_optional = models.CharField(max_length=500)
    optional = models.CharField(max_length=500)

class MainModel(models.Model):
    optional_fk = models.ForeignKey(
        FKModel, null=True, blank=True, on_delete=models.SET_NULL
    )

Now, let's imagine we have a test scenario for my MainModel which depends on the optional field from a optional_fk to have some random value so the test can pass.

Model mommy already solves this test setup scenario, but I think we could also be able to do so via _fill_optional with lookup fields. In my opinion, this would increase model mommy's API because we already can use lookup fields to create or prepare instances and recipes.

Expected behavior

My suggestions is to add the lookup feature also to the _fill_optional value. So, the following call would be valid:

obj = mommy.make(MainModel, _fill_optional=['optional_fk__optional'])
assert bool(obj.optional_fk.optional) is True

Actual behavior

Right now, model_mommy force us to manually create an instance of FKModel with the optional fields being populated and then to pass is to mommy.make when creating a new instance of MainModel. Here's how we have to code the same test scenario today:

optional_obj = mommy.make(FKModel, _fill_optional=['optional'])
obj = mommy.make(MainModel, optional_fk=optional_obj)
assert bool(obj.optional_fk.optional) is True
@berinhard berinhard transferred this issue from berinhard/model_mommy Oct 22, 2019
@ChillarAnand
Copy link

_fill_optional is not filling the optional fields of a related model(foreign key, onetoone, etc). Shouldn't it populate them also?

class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.SET_NULL, null=True, blank=True)

class Author(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(null=True, blank=True)

book = baker.bake(Book, _fill_optional=True)
assert book.author.address != ''

The last statement will fail as it is not filling optional fields on the related model.

@berinhard
Copy link
Member Author

I'm a +1 for that @ChillarAnand. Your comment is totally related to this issue, thanks for bringing it up.

@JustinC474
Copy link

+1 to both of these requests. I would have expected _fill_optional=True to cascade to related models.

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