Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在graphene python DjangObjectType中获取模型的当前实例_Python_Django_Graphene Python - Fatal编程技术网

如何在graphene python DjangObjectType中获取模型的当前实例

如何在graphene python DjangObjectType中获取模型的当前实例,python,django,graphene-python,Python,Django,Graphene Python,我有一个graphene python DjangObjectType类,我想添加一个自定义类型,但我不知道如何在解析器函数中获取当前模型实例。我在跟踪,但找不到任何参考资料 这是我的DjangObjectTypeClass: class ReservationComponentType(DjangoObjectType): component_str = graphene.String() class Meta: model = ReservationComp

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

这是我的DjangObjectTypeClass:

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

我的问题不同于,因为我的对象确实有一个模型。我不知道为什么它被标记为“可能重复”,有如此明显的差异。事实上,我的问题是基于模型的。

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

type\u model=info.parent\u type.graphene\u type.\u meta.model
但如果使用
djangObjectType
,则实例将传递给
self
。所以你可以走另一条路:

class ReservationComponentType(DjangObjectType):
component_str=graphene.String()
类元:
模型=保留组件
def解析组件(自身,信息):
#self已经是类型模型的实例(不确定是否在所有情况下都是):
组件类=自身类__
当前预订组件=info.get('预订组件')
component=当前\保留\组件。获取\组件()
返回组件名

@PetarP不,这不是一个重复的问题,因为我的对象确实有一个模型,我的问题确实基于这样的模型。你提到的问题与我试图解决的问题无关。对不起,没有正确阅读,将删除它。嘿,这里有一个很好的例子