valleymanbs
valleymanbs

Reputation: 487

Django ModelForm save() overloading

I am trying to define two different versions of the save() method for a ModelForm, but the only one being executed is the second one. When i try to save() i get error: save() takes at least 3 arguments, 1 given.

[...]
def save(self,commit=True):
    return super(NewProgramForm,self).save(commit=True)

def save(self, NameFile, SizeFile , commit=True):
    inst = super(NewProgramForm, self).save(commit=False)
    inst.size = SizeFile
    inst.sketches_file = NameFile
    inst.lines_of_code = 0

    if commit: inst.save()

    return inst

I'm quite sure the error is trivial, but i can't figure out what's wrong... Thank you in advance for any useful tip!

Upvotes: 0

Views: 141

Answers (1)

Ludwik Trammer
Ludwik Trammer

Reputation: 25052

Defining multiple methods with the same name, but different attributes is called method overloading and is popular in languages such as Java. This is not permitted in Python - plain and simple.

You need to change the name of the second save method, or merge the methods into one method with NameFile and SizeFile having a default value (making them optional).

Upvotes: 1

Related Questions