litestar-org/polyfactory

Allow setting the key value of a container type

Closed this issue · 2 comments

When we have this:

class Foo(BaseModel):
    name: str


class Foos(BaseModel):
    __root__: dict(str, Foo)

and this:

class FooFactory(ModelFactory):
    __model__ = Foo


class FoosFactory(ModelFactory):
    __model__ = Foos

There is no way (that I can tell) to customize the key value of the root model. I'd like to be able to do this

test_foos = FoosFactory.build(root_key='asdf_qewr_1234_wow', foo=FooFactory.build(name='omg'))  # root_key isn't a thing, but just pretend
print(test_foos.json(indent=2))

"""
this would output
{
  'asdf_qwer_1234_wow': {
      'name': 'omg'
  }
}
"""

Or maybe this:

class Foos_factory(ModelFactory):
    __model__ = Foos

    root_key = f'FOO#{ModelFactory._get_faker().uuid4()}'

I don't know if root_key is the right value here, but it would be very useful to have something to hook into to customize this value.

Despite the example I am not quite sure I understand the request.

should i close this or is there clarification incoming 😉 ?