简体   繁体   中英

Django - Signature of method does not match signature of base method in class

I am learning Django and I am following a lynda.com course. In one of there courses "building an elearning site", the have the following code:

class CourseModuleUpdateView(TemplateResponseMixin, View):
    template_name = 'courses/manage/module/formset.html'
    course = None

    def get_formset(self, data=None):
        return ModuleFormSet(instance=self.course,
                             data=data)

    def dispatch(self, request, pk):
        self.course = get_object_or_404(Course, id=pk, owner=request.user)
        return super(CourseModuleUpdateView, self).dispatch(request, pk)

    def get(self, request, *args, **kwargs):
        formset = self.get_formset()
        return self.render_to_response({'course': self.course,
                                        'formset': formset})

    def post(self, request, *args, **kwargs):
        formset = self.get_formset(data=request.POST)
        if formset.is_valid():
            formset.save()
            return redirect('manage_course_list')
        return self.render_to_response({'course': self.course,
                                        'formset': formset})

But I am getting an error message from PyCharm (my IDE) on:

def dispatch(self, request, pk):

And the error is:

Signature of method 'CourseModuleUpdateView.dispatch()' does not match signature of base method in class 'View' less... (Ctrl+F1) 
This inspection detects inconsistencies in overriding method signatures.

Is there a way for me to troubleshoot the issue and see how to begin fixing the error? What is Pycharm even trying to tell me??

I am using python 3 and DJango 1.11

You're overriding a method dispatch of the parent class View whose signature is def dispatch(self, request, *args, **kwargs): which you can see from yours does not match.

Signature here means that the method arguments should match with parent class method you're overriding.

Firstly you have to understand that this is a warning, not error.

Secondly: every argument (except of request ) that is passed to view by Django is extracted from URL, as defined in urlpatterns . Django is using *args and **kwargs internally in class-based views so you can pass any argument without need for overwriting dispatch method.

And back to our warning: this warning is raised by many of static code analysis tools (including one built into PyCharm) to inform you that something here may be wrong, because original dispatch method has different signature. It doesn't mean that this is wrong and that's why there are always options to mute those warnings on selected code lines. You should of course look at every warning your editor raises, but that doesn't mean that every warning should be fixed.

You can fix it of course using:

    def dispatch(self, request, *args, **kwargs):
        id = args[0] # or id = kwargs['id'] if it is passed as keyword argument
        self.course = get_object_or_404(Course, id=pk, owner=request.user)
        return super(CourseModuleUpdateView, self).dispatch(request, pk)

but you can also ignore that and use as is. Your usage has some benefits, for example automatic validation on method invoke that all required arguments have been passed. Usage with default method signature (as above) has benefit in not raising that warning in your editor. It is up to you to decide which one is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM