Open albertalexandrov opened 1 month ago
Thanks for providing the full code example.
It is, however, quite complex to read without prior knowledge of your project.
By default, with a RelatedFactoryList
, the behaviour is akin to:
ttz = Ttz(name="TTZ 1")
session.add(ttz)
for i in range(2):
session.add(TtzFile(ttz=ttz, file_name="some_file_name", attachment_id=SomeUUID()))
How would you write that piece of code without factories in order to get the files
attribute populated?
Hi, @rbarrois !
I would write like this:
files = []
for i in range(2):
file = TtzFile(file_name="some_file_name", attachment_id=SomeUUID())
files.append(file)
ttz = Ttz(name="TTZ 1", files=files)
session.add(ttz)
As far as I now factory boy first creates main object and then related list.
Your snippet wouldn't work, the ttz
is not created beforehand!
However, if that's the way you'd write it, I suggest using a factory.List
and a factory.SubFactory
:
class FileFactory:
...
class TtzFactory:
files = factory.List([
factory.SubFactory(FileFactory),
factory.SubFactory(FileFactory),
])
This might work, instantiating the two File objects before attaching them.
There was a mistake (copy paste). I fixed.
Does SubFactory(FileFictory) return a stub object? As you can see TtzFile cannot be created without Ttz.
Sorry, I can't check it because I don't have access to my computer. Well I ll try in a week.
Thanks! Can you try the approach I suggested above, i.e a list of subfactories instead of a RelatedFactoryList
?
I'll try later in a week when I reach my computer. Thanks.
Hi!
I have a question about using RelatedFactoryList in async SQLAlchemy. RelatedFactoryList creates instances but they are not attached to instance.
overridden for async base factory (from discussions in this repository):
models.py
factories.py
To make it available to get Ttz.files I have do refresh:
My question is it is the only way to get Ttz.files? I mean do I have to write _after_postgeneration method in each factory where I need to get related list?