Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow derive models from pydantic models #62

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

erny
Copy link
Contributor

@erny erny commented Dec 1, 2020

See #60. @art049: I create this draft PR to start discussing how this could be done. Thanks in advance.

@art049
Copy link
Owner

art049 commented Dec 22, 2020

Thanks for the amazing initiative @erny, i think we can maybe try to properly define the specs first and then we can check how it would be feasible.
For sustainability and isolation from pydantic internals, I think we should avoid to pass a custom Field descriptor (which actually generates a FieldInfo object) while building the initial pydantic models. This was a decision I took during the early ODMantic stage and I think it will really help to support future changes that might happen in pydantic itself.

From the example you added in the issue, I think we can try to build a use case taking in account some more customization. For example:

  • we want name to be a primary field: equivalent to a regular definition with Field(primary_field=True).
  • the founded field should be internally named founded_year in the DB documents: equivalent to a regular definition with Field(key_name="founded_year").
  • we want to be able to provide the ODMantic configuration object that will customize the collection name to "book_publishers" (currently, it's not really containing critical data but the future indexes will be defined there for example).

We keep the same business Model definition:

from pydantic import BaseModel, Field

class Publisher(BaseModel):
    name: str
    founded: int = Field(ge=1440)
    location: Optional[str] = None

The persistence model creation would look like this:

from odmantic import AIOEngine, Model, Field
from schemas import Publisher

class PublisherDB(Model, Publisher):
    class Config:
        collection = "book_publishers"
        fields = {
            "name": Field(primary_field=True),
            "founded": Field(key_name="founded_year")
        }

To avoid duplicating model configuration, everything related to pure ODMantic fields configuration would be stored in the fields config property.
As well, the resulting descriptors should take into account the ones from the base class. In this case, the resulting "founded" descriptor should contain both the ge=1440 information as well as the key_name information.

Finally, since we use the inheritance approach, we won't be able to have fewer fields in the DB model than in the Domain one. The opposite should be possible nevertheless and it would allow to add some more database only fields in the PublisherDB definition.

I hope this spec is filling the need you have and i'm obviously opened for some refinement 😉

@art049 art049 force-pushed the master branch 2 times, most recently from 5bd1656 to a99a258 Compare November 3, 2023 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create odmantic model from pydantic model
2 participants