0

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!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your answer! I admit I am a python newbie, and I had this doubt… I'll implement this another way, thanks again for the clarification.
Sure. One additional tip: There is a strong convention suggesting that pretty much everything that is not a class name (like method attributes) should start with a lowercase letter (more about naming conventions in Python)
Also, thanks for completing the answer with a suggestion for the implementation. Answer accepted, you're very kind!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.