-
-
Notifications
You must be signed in to change notification settings - Fork 420
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
[proposal] Models to Schemas #17
Comments
Hi @vitalik, I'm the maintainer of https://github.com/jordaneremieff/djantic (previously However, I've recently switched from experimental status to a more stable release, and I noticed you're doing some very similar work in this repo so I wanted to float the idea of collaboration at this time. I think the Would you like to share the effort here and become a co-maintainer of Djantic and it could serve as an upstream dependency to Django Ninja? My reasoning for proposing it as a dependency (rather than including it directly in this package itself) is because I think a package specific to the ORM could be used more generically in multiple frameworks and use-cases that aren't specifically related to JSON APIs. Please let me know what you think, cheers. |
Hi @vitalik, @jordaneremieff, and everyone, Thank you both for your respective efforts -- great stuff! As I have ebbed and flowed between Django/REST/Channels, FastAPI, Pydantic, Celery, and Kombu on the backend, and then embraced more use of async (e.g. websockets) on both GUI and non-GUI (e.g. CLI) frontends, the glaring problem is that "I want to declare my schema and core validation one time but I am not sure where to do it." Specifically, I would characterize the problem further as follows:
Thus, I think the Schema "Models" introduced by Pydantic are the best place to have the "single source of truth" exist, and we reverse the directionality of meta-inheritance. Consider the following:
From a DRY perspective, I would propose building as follows:
All put together: schema.py
models.py
models.py equivalent (sorry for lazy psuedo-code)
I look forward to helping with the development of these efforts. Thank you all for your consideration and feedback! |
Hi @jdzero But in my practice (and what I see from other people feedbacks) it's usually the other way round one django model - can produce multiple pydantic schemes:
on the other hand your approach can just a plugin/external library - pretty easy implementable.. |
Well that sounds good, but there is one slight problem that I do not see how to overcome a very "core" of the django ninja is the Even the naming (schema) is pretty important to avoid confusion between django models and pydantic models... maybe the class can be come dependency, but alias/naming not sure... |
Thanks for the feedback! I see what you are saying. Some terms (maybe helps with other topic as well):
The goal of your effort is to alleviate the effort of going from data models to API schemas, whereas what I am discussing is a new body of work upstream that gets you from data storage schemas into data models. Thanks again for your time, and please let me know if I can assist if you move out on any of these efforts! |
Hi @vitalik
Yeah, this is an important detail, and I've given it a lot of thought previously - eventually I landed on Another idea might also be to move the implementation of the existing |
Hi @vitalik, I've been working extensively with various Django based backend architectures and finally decided to give django-ninja a try. Seems pretty simple and intuitive and resembles a lot of FastAPI paradigms as quoted. After writing a bit of code, I found that having a pydantic schema centric approach kinda made more sense. Upon reading this discussion thread, I found that it was cited by @jdzero as well. To add to that, I was also considering making the ORM queries natively serialize into Pydantic schema instances rather than instances of class Pet:
name = CharField(max_length=100, empty=True)
age = PositiveIntegerField(null=True)
class PetSchema(BaseModel):
name: Optional[str] = None
age: Optional[conint(ge=0)] = Field(None, description='Age in years.')
class Config:
objects = Pet.objects
...
qs: QuerySet[PetSchema] = PetSchema.objects.all() The advantages of doing something like the above are as follows:
But in order to implement the above, support for serialising to a different type should be baked into the core Django ORM code. On the other hand, considering that the Django convention over the years has been to have To answer the main topic of discussion, personally I'd prefer explicitly declaring the Also, since both Let me know your thoughts on the same. Thanks. |
I'm looking into alternatives to DRF and just wanted to add my 2 cents. I definitely like the idea of leveraging the work already done in the djantic project -- seems like a very natural fit that would avoid duplicating efforts and make it easier to achieve full support for the many features of Django's models (eg. jordaneremieff/djantic#17, and jordaneremieff/djantic#3). As to the question posed in this issue, I would opt for |
Read full proposal here - http://django-ninja.rest-framework.com/proposals/models/
To make it possible to convert django models to ninja Schemas:
This feature will definitely land soon to production code
But there is just one open question:
configuration class - should it be
Meta
(django way) orConfig
(pydantic way) ?The text was updated successfully, but these errors were encountered: