Cannot pass id instead of related object to DjangoModelFactory
See original GitHub issueThe problem
When I create an instance of a Django model, I can pass a related object directly, or its id.
article = Article.objects.create(author=author)
article = Article.objects.create(author_id=author.pk)
One would expect a Django model factory to work the same way, but it doesn’t. Everything breaks and there is no documentation regarding this use case.
Proposed solution
Ideally allow both strategies, but if not at least document this and explain how to do it.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Getting id of associated child records in factory_boy
For your example, I'd go with the following factories: class FunctionFactory(factory.django.DjangoModelFactory): class Meta: model = models.
Read more >Pass id of generated object to SubFactory · Issue #763 - GitHub
I have the next piece of code: deliverer = factory.SubFactory( 'orders.tests.factories.DelivererFactory', company_made_id=factory.
Read more >Reference — Factory Boy stable documentation
If set, the object generated by the factory declaring the RelatedFactory is passed as keyword argument to the related factory. class CityFactory(factory.Factory): ...
Read more >Testing Models with Django using Faker and Factory Boy
you can add factory.Sequence. import factoryclass CompanyFactory(factory.DjangoModelFactory): """ | id | name | |:-- ...
Read more >Factory Boy Fun - Adam Johnson
... to fill your development database rather than dumping from production. ... JSON objects with data to be passed to the model constructor; ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Basically, factory_boy doesn’t care whether it’s working with IDs, instances, or anything else. It takes your declarations, computes the values (following subfactories, etc.), and sends the resulting payload as arguments and keyword arguments to the class you’ve provided for a model.
If you want a factory that will use a passed-in
author_id
if provided, but uses aSubFactory
to create anAuthor
if needed, you could use the following:This way:
author_id
has been provided, theMaybe
declaration will use its “else” branch and callAuthorFactory
to get an instance;author_id
(e.gArticleFactory(author_id=42)
), theMaybe
declaration will be skipped and only theauthor_id
will be passed toArticle.objects.create()
.author_id
andauthor
, the result will depend on Django’s implementation.The
_adjust_kwargs
function is useful in more complex cases, but is only called once the declarations have been resolved; it can’t prevent calling anAuthorFactory
earlier on, which might be an issue for your code.@MRigal Indeed. Feel free to open a pull request with that prose in there 😉