i8enn / pydantic-odm

Small async ODM for MongoDB based in Motor and Pydantic library
MIT License
39 stars 8 forks source link

ReferenceField analogue needed #18

Open Dogrtt opened 4 years ago

Dogrtt commented 4 years ago

Right now, if you want to store reference to another document, you should create something like this:

class Foo(DBPydanticMixin):
    names: List[str]

    class Config:
        database = 'default'
        collection = 'foo'

class MetaInfo(BaseDBMixin):
    flag: bool = True
    count: int  = 0
    foo: Optional[Foo] = None

class Bar(DBPydanticMixin):
    name: str
    meta: MetaInfo = MetaInfo()

    class Config:
        database = 'default'
        collection = 'bar'

But when you tried to save new Foo in field foo of Embedded model MetaInfo in Bar model you will faced with the fact, that Foo will be saved as separated document in foo collection and also will be duplicated as field of MetaInfo.