无法在 graphene_django 中获取 OneToOne 关系查询的值
Posted
技术标签:
【中文标题】无法在 graphene_django 中获取 OneToOne 关系查询的值【英文标题】:can't get the value of OneToOne relationship queries in graphene_django 【发布时间】:2020-09-29 20:35:27 【问题描述】:默认的 django 用户模型已使用 OneToOne 链接进行了扩展(添加了一些额外的字段)。
这是我的模型:
class Driver(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE
)
father_name = models.CharField(
max_length=20,
blank=True,
verbose_name="Father's Name"
)
national_id = models.CharField(
max_length=10,
blank=True,
verbose_name="National ID"
)
phone_no = models.CharField(
max_length=11,
blank=True,
verbose_name="Phone Number"
)
以下是我的架构(如果需要):
class DriverType(DjangoObjectType):
class Meta:
model = Driver
class Query(object):
all_drivers = graphene.List(DriverType)
def resolve_all_drivers(self, info, **kwargs):
return Driver.objects.all()
问题定义: django的默认User(django.contrib.auth.model
)有first_name、last_name、email等字段。根据User和Driver模型的OneToOne关系,预计访问该数据。
示例查询:
query
allDrivers
id
verified
user
first_name
错误信息:
"无法查询类型 \"DriverType\" 上的字段 \"user\"。",
【问题讨论】:
你有没有想过这个问题? @Corrodian 我现在无法访问我的计算机。我会在 12 小时内告诉你! 【参考方案1】:class DriverType(DjangoObjectType):
class Meta:
model = Driver
class UserType(DjangoObjectType):
class Meta:
model = User
class Query(object):
all_drivers = graphene.List(DriverType)
all_users = graphene.List(UserType)
def resolve_all_drivers(self, info, **kwargs):
return Driver.objects.all()
def resolve_all_users(self, info, **kwargs):
return Users.objects.all()
【讨论】:
【参考方案2】:您可以通过以下方式从与相关模型具有一对一关系的模型中查询数据。
您需要为您尝试访问的模型创建一个DjangoObjectType
,它会起作用。
这是我的models.py
from django.contrib.auth.models import User
from django.db import models
class UserModel(models.Model):
USER_TYPES = [
("CUSTOMER", "CUSTOMER"),
("AGENT", "AGENT"),
("ADMIN", "ADMIN"),
]
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_type = models.CharField(max_length=8, choices=USER_TYPES)
def __str__(self):
return f"P: self.user.username's profile"
这是我的schema.py
from api.models import UserModel
from django.contrib.auth.models import User as auth_user
from graphene_django import DjangoObjectType
import graphene
class User(DjangoObjectType):
class Meta:
model = UserModel
class UserType(DjangoObjectType):
class Meta:
model = auth_user
class Query(graphene.ObjectType):
hello = graphene.String(default_value="Hi!")
user = graphene.Field(User, id=graphene.Int())
def resolve_user(self, info, **kwargs):
id = kwargs.get("id")
if id is not None:
return UserModel.objects.get(pk=id)
return None
schema = graphene.Schema(query=Query)
这是我在Graph*i*QL
中运行的查询
query
user(id:3)
id
userType
user
firstName
lastName
email
username
id
我收到的回复
"data":
"user":
"id": "3",
"userType": "ADMIN",
"user":
"firstName": "John",
"lastName": "Doe",
"email": "admin@site.com",
"username": "admin001",
"id": "1"
希望这就是你要找的。p>
我的回复基于对此问题的回复。
https://github.com/graphql-python/graphene-django/issues/807#issuecomment-546310680
【讨论】:
以上是关于无法在 graphene_django 中获取 OneToOne 关系查询的值的主要内容,如果未能解决你的问题,请参考以下文章
在 graphene/graphene_django 中扩展查询参数
如何隐藏/排除graphene_django中请求实体的某些外键字段?
ModuleNotFoundError:没有名为“graphene_django”的模块