使用json模型字段和django graphene
我正在为我的项目使用graphql端点。其中一个模型具有包含一些json的textfield。如果我通过graphql请求我的实体列表,我得到的json就像一个字符串。如何在graphql中使用它作为嵌套结构,并具有过滤、选择某些属性等功能使用json模型字段和django graphene,django,django-models,graphql,python-3.6,graphene-python,Django,Django Models,Graphql,Python 3.6,Graphene Python,我正在为我的项目使用graphql端点。其中一个模型具有包含一些json的textfield。如果我通过graphql请求我的实体列表,我得到的json就像一个字符串。如何在graphql中使用它作为嵌套结构,并具有过滤、选择某些属性等功能 class SysObjects(models.Model): id = models.BigAutoField(primary_key=True) user_id = models.BigIntegerField() type_id
class SysObjects(models.Model):
id = models.BigAutoField(primary_key=True)
user_id = models.BigIntegerField()
type_id = models.PositiveIntegerField(blank=True, null=True)
# status = models.ForeignKey('SysObjectsStatuses', models.DO_NOTHING, blank=True, null=True)
title = models.CharField(max_length=255, blank=True, null=True)
data = models.TextField(blank=True, null=True) #json string is here
visible = models.IntegerField()
date_actual = models.DateTimeField()
date_update = models.DateTimeField(blank=True, null=True)
date_add = models.DateTimeField(blank=True, null=True)
orig = models.CharField(max_length=255, blank=True, null=True)
is_color = models.PositiveIntegerField()
class Meta:
managed = False
db_table = 'sys_objects'
app_label = "default"
def __str__(self):
return self.title
您可以使用自定义类型为数据字段声明解析器,例如
import graphene
from graphene.django.types import DjangoObjectType
class SysObjectsType(DjangoObjectType):
data = DataType()
class Meta:
model = SysObjects
def resolve_data(self, info):
# What you return here depends on how you are unpacking the JSON data
return self.data
class DataType(graphene.ObjectType):
# What you put here depends on how you want to structure the JSON output
...
您可能能够扩展数据类型数据的这种想法,以返回数据类型数据,从而能够处理任意深度的数据树