我在一个小的Django
应用程序上工作,得到一个错误告诉我,super(type, obj): obj must be an instance or subtype of type
。在引入函数get_object_or_404
之后,我从views.py
文件中获得它。下面提供的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
models.py
中的line 18
是qs_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'
有人能给我解释一下错误的原因和解决方法吗?如果需要的话,我愿意提供更多的信息。
另一种可能发生此错误的方式是在Jupiter笔记本中用类重新加载模块时。
简单的解决方案是重新启动内核。
http://thomas-cokelaer.info/blog/2011/09/382/
查看@Mike W的答案了解更多细节。
应该使用
UrlManager
类作为第一个参数而不是URL
模型来调用super
。super
不能用不相关的类/类型调用be:从医生那里
所以你不能:
你应该:
或者在Python 3中:
在@OĝuzŞerbetci的答案中,在python3中(不仅在Jupyter中是必需的),当需要重新加载库时,例如我们将
class Parent
和class Child
定义为如果你这样做
您将得到
TypeError: super(type, obj): obj must be an instance or subtype of type
,解决方案是在重新加载之后重新导入类相关问题 更多 >
编程相关推荐