I have multiple models in Django named model1
, model2
, model3
.
Each model has the same fields and the field id
is the primary key.
I want to query this database using GraphQL. The query structure should be like below.
query{
pod(model: "model1",id: "1")
{
id
data1
data2
}
}
This should get me the object for id 1 from model 1. How can I do this in Django using Graphene ?
Create a map for your models and how you want it to be accessed from your graphql query. Do it like so:
def resolve_pod(self, root, info, model, id):
model_map = {'model1': Model1, 'model2': Model2, 'model3': Model3}
if model in model_map.keys():
return model_map[model].objects.get(id=1)
return None