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

I'm working on a small application Djangoand get an error message super(type, obj): obj must be an instance or subtype of type. I get it from the file views.pyafter introducing the function get_object_or_404. The file views.pybelow

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()

the full error message is displayed here,

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

B models.py line 18is located qs_main = super(URL, self).all(*args, **kwargs), and the file models.pyis 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 cause of the error for me and how to solve it? I am open to providing additional information if required.

+22
source share
4 answers

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)
+15

@Oğuz Şerbetci, python3 ( Jupyter), , , class Parent class Child

class Parent(object):
    def __init__(self):
        # do something

class Child(Parent):
    def __init__(self):
        super(self,Child).__init__(self)

,

import library.Child
reload(library)

Child()

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

import library.Child
reload(library)
import library.Child

Child()
+8

, , ,

class A(Foo):
    def __init__(self):
        super(A, self).__init__()
        #...

class A(Foo):
    def __init__(self):
        super(A, self).__init__()
        #...

A, super, __init__ A A, . - , - , .

-, , , . , , , , .

+4

Source: https://habr.com/ru/post/1676214/


All Articles