[go: up one dir, main page]

Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Combine model and schema #37

Open
jordaneremieff opened this issue Sep 3, 2021 · 3 comments
Open

Combine model and schema #37

jordaneremieff opened this issue Sep 3, 2021 · 3 comments
Labels
maybe A feature or request that may or may not be implemented

Comments

@jordaneremieff
Copy link
Owner
jordaneremieff commented Sep 3, 2021

The approach in https://github.com/tiangolo/sqlmodel has me interested exploring a combined model for Django and Pydantic. I would consider doing a PR to that project, though it seems too specific to SQLAlchemy and FastAPI for a Django-specific implementation to be a good fit (also don't know if I'll pursue this at all) - not sure.

Something like this for the model definition:

from djantic.models import DjanticModel, Field

class Plant(DjanticModel):
    """A plant."""

    common_name: str
    genus_name: str = Field(
        max_length=100,
        help_text="A generic name for a collective group of plants.",
    )
    specific_epithet: str

    class Meta:
        ordering = ["genus_name"]

    class Config:
        include = ["genus_name", "specific_epithet"]

Then attribute access would (ideally) look like this:

from myapp.models import Plant

# Schema definition from class
schema = Plant.schema()

# Export data from instance
plant = Plant.objects.create(genus_name="Grevillea", specific_epithet="buxifolia")
data = plant.dict()

Maybe.

@jordaneremieff jordaneremieff added the maybe A feature or request that may or may not be implemented label Sep 3, 2021
@adamghill
Copy link

I'm not sure if it's possible to solve 100% of what can be done with a Django model, but I've gotten a little spoiled by pydantic and every time I write a Django model I think about what could be possible with type annotations. +1 for trying to tackle this either by extending SQLModel or separately!

@andyreagan
Copy link

You should do it!

@jacekjursza
Copy link

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
maybe A feature or request that may or may not be implemented
Projects
None yet
Development

No branches or pull requests

4 participants