7.3.9 Updating Objects with ModelForm
When we inherited ModelForm
, we were able to remove our implementation of save()
, because ModelForm
provides its own. However, unlike our simple implementation, the ModelForm
version of save()
allows for objects to be created and updated.
The inheritance of ModelForm
allows us to pass objects to the subclassed form via the instance
attribute, as shown in Example 7.23.
>>> django_tag = Tag.objects.get(slug='django') >>> form = TagForm(instance=django_tag)
At the moment, if we run save()
, nothing will happen because the data has not changed, and neither has the state of the form, as shown in Example 7.24.
Get Django Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.