0

I am developing a application in Django. I am new to python and facing a issue while calling a super call method from a subclass. I am using Python 3.

This is my sunclass :

class TestClass(BaseController):
    def try_base(request: HttpRequest):
        return super().send_response(200, "", "", model1())

And this is my super class

class BaseController:

    def convert_to_dict(obj):
        return obj.__dict__

    def send_response(self, http_status, error_code, error_message, response_object):
        obj = BaseResponse(http_status, error_code, error_message, response_object)
        data = json.dumps(obj, default=convert_to_dict, indent=4)
        return HttpResponse(data)

I don't know what exactly the problem. It always gives me an error

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

Please help me in resolving this.

Nikesh Kedlaya
  • 652
  • 4
  • 10
  • 30
  • are you sure you are using python 3 ? the error you get is for python 2 not for python 3 – Ahtisham Jan 29 '19 at 12:13
  • I am using Python 3.7.2 – Nikesh Kedlaya Jan 29 '19 at 12:24
  • Are you using this as a view? Where are you calling it? Show the urls.py. (And explain why you've created your own class-based views pattern rather than using the one Django provides, especially as you're new to both Python and Django.) – Daniel Roseman Jan 29 '19 at 13:39

1 Answers1

3

Your method needs to have self as its first parameter:

class TestClass(BaseController):
    def try_base(self, request: HttpRequest):
        return super().send_response(200, "", "", model1())

All methods require self as the first argument.

Also you don't need to use super() at all for this. Just call self.send_response(200, "", "", model1()) directly. super() is only needed when you want to call a method of the same name in a base class.

class TestClass(BaseController):
    def try_base(self, request: HttpRequest):
        return self.send_response(200, "", "", model1())

As for why you get that particular error, I'm not sure because your code doesn't give me that error. It may be as suggested in the comments that you are actually running an old version of Python.

From the comment discussion, I think you must have set things up so that Django is calling TestClass.try_base(obj) instead of calling try_base() on an instance of TestClass. That also explains why super() didn't work for you: it requires the first parameter of the method to be getting an instance as its first parameter. You cannot call send_response() which is an instance method unless you have an instance on which to call it.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • But if i use self, then django will send request s first param so i cannot use self as a first parameter. That is why i have not used self anywhere in here – Nikesh Kedlaya Jan 29 '19 at 12:35
  • It doesn't matter what Django is doing, any instance method must declare `self` as its first parameter: it is inserted automatically when you call the method on an instance. – Duncan Jan 29 '19 at 12:43
  • If i put self as first parameter then i get other error as `'WSGIRequest' object has no attribute 'send_response'` as django passes this object as first parameter. – Nikesh Kedlaya Jan 29 '19 at 12:45
  • 1
    I've updated my answer. I think your problem is however you've told Django to call the `try_base()` method: you are calling it directly on the class rather than on an instance. That's where the real problem is. – Duncan Jan 29 '19 at 12:55
  • Solved. Used class based views. Thank you! – Nikesh Kedlaya Jan 30 '19 at 04:25