Replies: 1 comment
-
I think this is expected behaviour. If want to use a default on subfactory without setting from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel, Field
class Dog(BaseModel):
name: str
barks: str = Field("Woof")
class DogFactory(ModelFactory[Dog]):
__use_defaults__ = True
class Pets(BaseModel):
dog: Dog
class PetsFactory(ModelFactory[Pets]):
dog = DogFactory
d = PetsFactory.build().dict()
print(d) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Just writing, perhaps as a note to future self, that
__use_defaults__
did not quite work as my intuition expected it to.Mainly: it must be used on the outermost factory, i.e. to make
Dog
memeber varialbebarks = "Woof"
,__use_defaults__
must be declared inPetsFactory
, notDogFactory
, as I initially assumed.code example
(Simplified toy example of equivalent problem)
For future self: I also had a bug where it still wouldn't "bark", and instead generate random string, as I'm also forced to use
__set_as_default_factory_for_type__
for otherclassmethods
. I relaize now that then it must also be combined with__use_defaults__
:Hmm, interesting, in above code, I don't need
__use_defaults__
onPetsFactory
Perhaps this is to help to some future person.
Beta Was this translation helpful? Give feedback.
All reactions