Skip to content

Ability to exclude previous values from next model generation #1132

@andy-isoc

Description

@andy-isoc

The problem

We have a particular use case where we want to build one fixture with random values and then build another fixture but with certain properties that are guaranteed different from the previous fixture (but still "random").

e.g.

first_fixture = ModelThatReferencesCountryFactory()
second_fixture = ModelThatReferencesCountryFactory()
first_fixture.country_code != second_fixture.country_code

For some types of property a sequence would solve this problem but with country codes (we use ISO8601 2-letter country codes) that doesn't really work.

Proposed solution

I don't know enough about the internals to know how this might work but perhaps there could be a generic way of applying the solution as posted in #645

Or, imitating Django ORM queries

first_fixture = ModelThatReferencesCountryFactory()
second_fixture = ModelThatReferencesCountryFactory(country_code__ne=first-fixture.country_code)

Extra notes

Fakers .unique provides this but (as far as I can see) that cannot be used in FactoryBoy. I see there is a similar discussion in #762 and one of the constraints mentioned there is having a fixed set of values to pick from. In our case we are generally dealing with single figure amounts of fixtures so it would fine to throw an error if values were exhausted.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions