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

编程入门 行业动态 更新时间:2024-10-27 10:20:56
本文介绍了super(type,obj):obj必须是type的实例或子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个小型 Django 应用程序上工作,并收到一条错误消息,告诉我 super(type,obj):obj必须是实例或子类型类型。在引入函数 get_object_or_404 之后,我从 views.py 文件中获得了它。下面提供的 views.py 文件,

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 views.py file after introducing the function get_object_or_404. The views.py file provided below,

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: 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

第18行在 models.py 中是 qs_main = super(URL,self).all(* args,** kwargs)和 models.py 文件在此处,

The line 18 in the models.py is qs_main = super(URL, self).all(*args, **kwargs) and the models.py file is here,

# 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'

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

Can someone explain the the reason of error to me and how to solve it? I'm open to provide more informations IF required.

推荐答案

您应该致电 super 使用 UrlManager 类作为第一个参数,而不是 URL 模型。 super 不能与 unrelated 类/类型一起调用:

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

从文档中,

super(type [,object-or-type]) :返回将方法调用委托给父级或$的代理对象b $ b同级类。

super(type[, object-or-type]): Return a proxy object that delegates method calls to a parent or sibling class of 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中:

Or in Python 3:

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

更多推荐

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

本文发布于:2023-11-05 15:59:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   类型   super   type   obj

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!