I'm trying to understand the difference between Django's ModelForm save method and saving the Model instance directly.
Personally, I find saving directly more intuitive and more clearly shows when the data is saved. Plus, if I need to modify the instance before saving, then I have to use the Model save method as the Django documentation explains here.
So, once the form is validated, what is the difference? Would there be a difference if the form used multiple models or some other more complex use case?
I'm using Django version 1.4 if that matters. And below is some code showing how I tend to save validated form data.
Thanks in advance!
# models.py
class Project(models.Model):
project_name = models.CharField(unique=True, null=False, blank=False)
# views.py
def add_project(request):
if request.method == 'POST':
project = Project()
form = ProjectForm(request.POST, instance=project)
if form.is_valid():
project.save() ### <-- project.save() vs form.save() ###
return HttpResponseRedirect(reverse('view_project', args=(project.id,)))
else:
form = ProjectForm()
return render_to_response(
'add_project.html',
{
'form': form,
},
context_instance=RequestContext(request)
)
# forms.py
class ProjectForm(ModelForm):
class Meta:
model = Project
The main difference between the two is that in forms that are created from forms. ModelForm , we have to declare which model will be used to create our form. In our Article Form above we have this line " model = models. Article " which basically means we are going to use Article model to create our form.
Django ModelForm is a class that is used to directly convert a model into a Django form. If you're building a database-driven app, chances are you'll have forms that map closely to Django models. For example, a User Registration model and form would have the same quality and quantity of model fields and form fields.
str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.
Creating objects To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.
In the commented line you have, project.save()
simply won't do anything. The instance has not been updated with the form data, it is simply the empty instance you created two lines before. The only way to update an existing instance is by saving its form.
ModelForm.save()
returns an object saved from the data that was put into the form, Model.save()
returns an object from the data that the object was initialized with or values that were set after it was created. So when it comes to getting the data from what the user inputted on the form to a persisted object, it makes more sense to call ModelForm.save()
rather than going through the work of validating the data yourself, initializing the object and then saving it because all that work is handled by the ModelForm
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With