How to get dict dump of Factory
See original GitHub issueHello,
I need to get a full dump of a Factory as a dict so that I can serialize it to json. FactoryName.attributes()
works great except when there is a SubField
, in which case it evaluates it and returns an instantiated version of the object as opposed to a dictionary of key: value of the SubField
attributes.
I tried to hack around this by traversing the Factory
myself but it turns out that I’ll have to duplicate a lot of code that already exists. Do you see an easy way to do this?
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
How to convert defaultdict of defaultdicts [of defaultdicts] to dict ...
You can recurse over the tree, replacing each defaultdict instance with a dict produced by a dict comprehension:
Read more >Python Json | Json loads | Json dumps | Dictionary - YouTube
Python Json | Json loads | Json dumps | Dictionary ... is right one to convert those json string to dictionary for easy...
Read more >Common recipes — Factory Boy stable documentation
Pass that dict as keyword arguments to the model's build / create function. In order to get a dict, we'll just have to...
Read more >The Factory Method Pattern and Its Implementation in Python
Let's begin refactoring the code to achieve the desired structure that uses the Factory Method design pattern. Refactoring Code Into the Desired Interface....
Read more >Search — Python 3.7.14 documentation
...that remembers the order entries were added defaultdict dict subclass that calls a factory function to supply missing values UserDict wrapper around ...
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
@rbarrois
I am trying to use the
factory.build(dict, FACTORY_CLASS=MyFactory)
method in my REST API tests. However, the dictionary returned by that method contains a reference to an instance of an object inSubFactory()
fields:I think it would be better if this would return the ID of this object instance instead.
Is there any built-in method that does this?
@hectorcanto Great way!
I used that code as a reference and created a function that also outputs SubFactory as a dict.
We look forward to helping you.