Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django forms.ModelForm, Pylint, and new/old style classes

I have a Django 1.5 form that looks like this (simplified):

class BidForm(forms.ModelForm):
    class Meta:
        fields = (
        )
        model = Bid

    def __init__(self, *args, **kwargs):
        super(BidForm, self).__init__(*args, **kwargs)
        something()

When I run Pylint on this, I get a this error:

E1002:<line,row>:BidForm.__init__: Use of super on an old style class

I assume this means the Django's forms.ModelForm is an old-style class and per the python docs my call to super is not happening and is therefore extraneous. Is this true? Can I just delete the super call without effect?

like image 357
Erik Avatar asked Mar 06 '13 20:03

Erik


People also ask

What is the difference between forms and model forms in Django?

The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database. Save this answer.

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is super class of Django form?

Django provides a Form class which is used to create HTML forms. It describes a form and how it works and appears. It is similar to the ModelForm class that creates a form by using the Model, but it does not require the Model.


2 Answers

No. Pylint, great though it is, is far from infallible, and in this case has just got it wrong. ModelForm is a new style class and the super is needed.

like image 131
Daniel Roseman Avatar answered Oct 23 '22 20:10

Daniel Roseman


This error/warning has nothing to do with the ModelForm class and has to do with:

    class Meta:
        fields = ()
        model = Bid

You just need to suppress the warning:

    class Meta:  # pylint: disable=C1001
        fields = ()
        model = Bid
like image 22
Freddie Avatar answered Oct 23 '22 19:10

Freddie