没有Django模型的石墨烯Django? [英] Graphene Django without Django Model?

查看:98
本文介绍了没有Django模型的石墨烯Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用Graphene-Django成功构建了多个GraphQL调用。在所有这些情况下,我全部或部分填充Django模型,然后返回填充的记录。

I've successfully used Graphene-Django to successfully build several GraphQL calls. In all of those cases I populated, in whole or in part, a Django model and then returned the records I populated.

现在,我遇到了一种情况返回一些我不希望存储在Django模型中的数据。

Now I have a situation where I'd like to return some data that I don't wish to store in the Django model. Is this possible to do with Graphene?

Robert

推荐答案

Robert_LY在评论中完美地回答了他自己的问题,我想扩展他的解决方案。

Robert_LY answered his own question perfectly in the comments, I'd just like to expand his solution.

我的无数据库模型WordForm是自动生成的,而无需将其存储在数据库中。我将其定义为Django模型,如下所示:

My database-less model WordForm is generated automatically, without storing it in a database. I define it as a Django model as follows:

from django.db import models
class WordForm(models.Model):
    value = models.CharField(max_length=100)
    attributes = models.CharField(max_length=100)

在模式中,我定义节点并进行如下查询:

In the schema I define the node and query like this:

class WordFormNode(DjangoObjectType):
    class Meta:
        model = WordForm
        interfaces = (relay.Node, )

class Query(AbstractType):
    word_forms = List(WordFormNode,query=String(),some_id=String())

    def resolve_word_forms(self, args, context, info):
        query= args['query']
        some_id = from_global_id(args['some_id'])[1]
        word_forms = []
        # some logic to make WordForm objects with
        # WordForm(value=value,attributes=attributes),
        # then append them to list word_forms
        return word_forms

您可以根据需要向列表传递任意数量的参数,并以resolve_word_forms形式访问它们。

You can pass as many arguments as you like to the List and access them in resolve_word_forms.

这篇关于没有Django模型的石墨烯Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆