【问题标题】:Cannot query foreign key field in GraphQL无法在 GraphQL 中查询外键字段
【发布时间】:2020-02-19 04:17:24
【问题描述】:

我正在为 GraphQL 使用 graphene-django 框架。 除外键外,我可以检索的所有字段

# models.py

from django.db import models
from django.contrib.auth.models import User
from users.models import UserProfile

class Video(models.Model):
    title = models.TextField()
    description = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)      
# schema.py

class VideoType(DjangoObjectType):
    class Meta:
        model = Video

我的查询是这样的

query {
  home_videos {
    title
    description
    author
  }
}

以下是 GraphQLView 中的错误消息。

无法在 VideoType 类型上查询字段作者

【问题讨论】:

    标签: django graphql graphene-django


    【解决方案1】:

    我添加了 UserType 并更改了模型 foreignKey 类型

    class UserType(DjangoObjectType):
        class Meta:
            model = UserProfile
    
    author = models.ForeignKey(UserProfile, on_delete=models.CASCADE)    
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2016-03-03
      • 2020-02-28
      • 2020-12-04
      • 2021-11-13
      • 2019-06-11
      • 1970-01-01
      • 2021-06-25
      • 2016-12-05
      相关资源
      最近更新 更多