[Answer]-Form validation syntax – calling form.save() versus foo.save()

1👍

From Django’s website, probably this will clear up what you should do:

# Create a form instance with POST data.
>>> f = AuthorForm(request.POST)

# Create, but don't save the new author instance.
>>> new_author = f.save(commit=False)

# Modify the author in some way.
>>> new_author.some_field = 'some_value'

# Save the new instance.
>>> new_author.save()

# Now, save the many-to-many data for the form.
>>> f.save_m2m()

From https://docs.djangoproject.com/en/1.6/topics/forms/modelforms/

This is extracted from the source code of Django 1.6.2:

if commit:
    # If we are committing, save the instance and the m2m data immediately.
    instance.save()
    save_m2m()
else:
    # We're not committing. Add a method to the form to allow deferred
    # saving of m2m data.
    form.save_m2m = save_m2m
return instance

This is the reason why if after Ex1:form.save() you call bar.save_m2m() everything will be fine, you will be executing a normal save() step by step. The Base class is returning its instance each time so if you call save() it will save the same instance you are pointing to from outside and that you just modified (Warning: are you using it as a global variable?)…and anyway you can not be sure for how long this method will work as you expect since nowhere in the documentation says the instance returned when calling form.save(commit=False) is the same instance used when you call form.save() few lines below.

As far as I know you wanted to know which way is better to avoid future problems…my recommendation is: go with the second one!.

Leave a comment