super(type, obj): obj must be an instance or subtype of type

前端 未结 5 1305
天涯浪人
天涯浪人 2020-12-11 00:34

I work on a small Django app and get an error tells me, super(type, obj): obj must be an instance or subtype of type. I get it from the view

5条回答
  •  清歌不尽
    2020-12-11 01:04

    You should call super using the UrlManager class as first argument not the URL model. super cannot called be with an unrelated class/type:

    From the docs,

    super(type[, object-or-type]): Return a proxy object that delegates method calls to a parent or sibling class of type.

    So you cannot do:

    >>> class D:
    ...    pass
    ... 
    >>> class C:
    ...    def __init__(self):
    ...        super(D, self).__init__()
    ... 
    >>> C()
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 3, in __init__
    TypeError: super(type, obj): obj must be an instance or subtype of type
    

    You should do:

    qs_main = super(UrlManager, self).all(*args, **kwargs)
    

    Or in Python 3:

    qs_main = super().all(*args, **kwargs)
    

提交回复
热议问题