首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在石墨烯-python DjangoObjectType中获取当前模型实例

如何在石墨烯-python DjangoObjectType中获取当前模型实例
EN

Stack Overflow用户
提问于 2019-04-05 06:26:52
回答 1查看 583关注 0票数 2

我有一个graphene-python DjangoObjectType类,我想添加一个自定义类型,但是我不知道如何在解析器函数中获得当前的模型实例。我在跟踪本教程,但找不到任何参考资料。

这是我的DjangoObjectTypeClass

代码语言:javascript
复制
class ReservationComponentType(DjangoObjectType):
    component_str = graphene.String()

    class Meta:
        model = ReservationComponent

    def resolve_component_str(self, info):
        # How can I get the current ReservationComponent instance here?. I guess it is somewehere in 'info', 
        # but documentation says nothing about it

        current_reservation_component = info.get('reservation_component')
        component = current_reservation_component.get_component()

        return component.name

我的问题不同于没有模型的对象的石墨烯解析器,因为我的对象确实有一个模型。我不知道为什么它被标记为“可能的重复”,有如此明显的区别。我的问题是,确实是基于这种模式。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 14:19:38

是的,它在info的某个地方,即这里:

代码语言:javascript
复制
type_model = info.parent_type.graphene_type._meta.model

但是,如果使用DjangoObjectType,则实例将传递给self。所以你可以换种方式:

代码语言:javascript
复制
class ReservationComponentType(DjangoObjectType):
    component_str = graphene.String()

    class Meta:
        model = ReservationComponent

    def resolve_component_str(self, info):
        # self is already an instance of type's model (not sure if it is in all cases):
        component_class = self.__class__

        current_reservation_component = info.get('reservation_component')
        component = current_reservation_component.get_component()

        return component.name
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55529402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档