Mapping to models #1271
EmilienDreyfus
started this conversation in
Ideas
Replies: 1 comment
-
Is that what your looking for? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi dears,
First, I want to thanks all the team of graphene for the incredible work that is done. It's a wonderful tool and I'd like to contribute in my way, so here is a proposal:
My Django models are quite complicated and I'd like to expose more simple models to the external world. For example, I have models like thats:
There can be many reason I made a separation between Car and CarData, but for me its for performance reasons or simply for decoupling things.
An API is a 'view' of my models for the external world, it has not to reflect the reality of my models. However, I have to use them to build my api and I'd like to be able to map the 'view' I want to give to my actual model.
For example I'd like to show it flat like that:
Car:
For the moment, to make my API I have to make something like that:
So in fact, because I need to simplify the view of my models (And for me, a view, or an API is a simplification of a model), I can't benefit anymore of the automatic behavior of graphene, such as turning the choices as an enum automatically, defining automatically what type is value1 etc..
You can see that I also used my magic tool to rename the field value1 to value, which is quite handful!
For me, an API doesn't have to reflect the complexity of a model, and we shouldn't loose all the magic when we are in cases like that, which happens all the time. This kind of notation let me also stay DRY (And I my age its better).
So I'd like to discuss about what you think of my point of view, the functionality i'd like to have, maybe I missed something that would help me achieving what I want. If you like it, we can discuss about the best form (Solution 1: MappedField, Solution 2: reapeting type (allow name mapping) or solution 3: tuple for source), and I will write my first PR to join the team!
Best regards, and thanks in advance for all the readers and commenters.
Beta Was this translation helpful? Give feedback.
All reactions