Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mapping to models #1271

EmilienDreyfus started this conversation in Ideas
Discussion options

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:

class Car(models.Model):
 name = Charfield(max_length=123)
 data = OneToOne(CarData)
class CarData(models.Model):
 value1=models.CharField(choices=foo)

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:

  • name
  • value1

For the moment, to make my API I have to make something like that:

class CarType(DjangoObjectType):
 value1 = Enum(...)
 def resolve_value1(self, info):
 return self.data.value1
 class Meta:
 model = Ingredient
 fields = ("id", "name", "value1")

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..

  1. I'd like to be able to make something like that (source is a keyword inspired by DRF):
class CarType(DjangoObjectType):
 value = MappedField(source='data.value1')
 class Meta:
 model = Ingredient
 fields = ("id", "name", "value")
  1. More simply by repeating the type (but we loose the Enum magic):
class CarType(DjangoObjectType):
 value = Enum(foo, source='data.value1') 
 class Meta:
 model = Ingredient
 fields = ("id", "name", "value")
  1. or something like this:
class CarType(DjangoObjectType):
 class Meta:
 model = Ingredient
 fields = ("id", "name", ("value", "data.value1"))

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.

You must be logged in to vote

Replies: 1 comment

Comment options

Is that what your looking for?

image

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /