super(type,obj):obj必须是type的实例或子类型

我在一个小型Django应用程序上工作,并收到一条错误消息,告诉我super(type, obj): obj must be an instance

or subtype of

typeviews.py引入函数后,我从文件中获取了它get_object_or_404。下面views.py提供的文件,

from django.shortcuts import render, get_object_or_404    

from django.http import HttpResponse, HttpResponseRedirect

from django.views import View

from .models import URL

# function based view

def redirect_view(request, shortcode=None, *args, **kwargs):

obj = get_object_or_404(URL, shortcode=shortcode)

return HttpResponse("Hello World, the shortcode is {shortcode}".format(shortcode = obj.url))

# class based view

class ShortenerView(View):

def get(self, request, shortcode=None, *args, **kwargs):

obj = get_object_or_404(URL, shortcode=shortcode)

return HttpResponse("Hello World 1, the shortcode is {shortcode}".format(shortcode = obj.url))

def post(self, request, *args, **kwargs):

return HttpResponse()

完整的错误消息在这里,

TypeError at /b/p6jzbp/

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

Request Method: GET

Request URL: http://127.0.0.1:8000/b/p6jzbp/

Django Version: 1.11

Exception Type: TypeError

Exception Value:

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

Exception Location: /Users/Chaklader/Documents/Projects/UrlShortener/src/shortener/models.py in all, line 18

line 18models.pyqs_main = super(URL, self).all(*args,

**kwargs)models.py文件是在这里,

#  will look for the "SHORTCODE_MAX" in the settings and 

# if not found, will put the value of 15 there

SHORTCODE_MAX = getattr(settings, "SHORTCODE_MAX", 15)

class UrlManager(models.Manager):

def all(self, *args, **kwargs):

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

qs = qs_main.filter(active = True)

return qs

def refresh_shortcodes(self, items = None):

qs = URL.objects.filter(id__gte=1)

new_codes = 0

if items is not None and isinstance(items, int):

qs = qs.order_by('-id')[:items]

for q in qs:

q.shortcode = create_shortcode(q)

print (q.id, " ", q.shortcode)

q.save()

new_codes += 1

return "# new codes created {id}".format(id = new_codes)

class URL(models.Model):

url = models.CharField(max_length = 220, )

shortcode = models.CharField(max_length = SHORTCODE_MAX, blank = True, unique = True)

updated = models.DateTimeField(auto_now = True)

timestamp = models.DateTimeField(auto_now_add = True)

active = models.BooleanField(default = True)

objects = UrlManager()

def save(self, *args, **kwargs):

if self.shortcode is None or self.shortcode == "":

self.shortcode = create_shortcode(self)

super(URL, self).save(*args, **kwargs)

def __str__(self):

return str(self.url)

def __unicode__(self):

return str(self.url)

# class Meta:

# ordering = '-id'

有人可以向我解释错误的原因以及如何解决吗?如果需要,我愿意提供更多信息。

回答:

您应该super使用UrlManager类作为第一个参数而不是URL模型来调用。super不能使用不 相关的 类/类型进行调用:

从文档中

super(type[, object-or-

type]):返回将方法调用委托给类型的父级或同级类的代理对象。

因此,您 执行以下操作:

>>> class D:

... pass

...

>>> class C:

... def __init__(self):

... super(D, self).__init__()

...

>>> C()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 3, in __init__

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

你应该做:

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

或在Python 3中:

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

以上是 super(type,obj):obj必须是type的实例或子类型 的全部内容, 来源链接: utcz.com/qa/409710.html

回到顶部