nis*_*ish 3 django graphene-python graphene-django
所以我的模型看起来像
class Abcd(models.Model):
name = models.CharField(max_length=30, default=False)
data = models.CharField(max_length=500, blank=True, default=False)
Run Code Online (Sandbox Code Playgroud)
需要在查询时传递一个字典,它不是模型的一部分,查询是
query {
allAbcd(Name: "XYZ") {
edges {
node {
Data
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何通过查询传递这样一个自定义字段?
此自定义字段对于其他流程目的是必需的。
石墨烯使用类型来解析节点,这些节点完全不与模型绑定,您甚至可以定义不与任何模型关联的石墨烯类型。无论如何,您正在寻找的用例非常简单。假设我们有一个模型名称User,我假设这Data需要由模型的解析器来解析。
from graphene.relay import Node
from graphene import ObjectType, JSONField, String
from graphene_django import DjangoObjectType
from app.models import User
class UserType(DjangoObjectType):
class Meta:
filter_fields = {'id': ['exact']}
model = User
custom_field = JSONField()
hello_world = String()
@staticmethod
def resolve_custom_field(root, info, **kwargs):
return {'msg': 'That was easy!'} # or json.dumps perhaps? you get the idea
@staticmethod
def resolve_hello_world(root, info, **kwargs):
return 'Hello, World!'
class Query(ObjectType):
user = Node.Field(UserType)
all_users = DjangoFilterConnectionField(ProjectType)
Run Code Online (Sandbox Code Playgroud)