The save method is an inherited method from
models.Model which is executed to save an instance into a particular Model. Whenever one tries to create an instance of a model either from admin interface or django shell, save() function is run. We can override save function before storing the data in the database to apply some constraint or fill some ready only fields like SlugField.
Technically it is not recommended to override the save method to implement such functionalities because any error in save method lets to crash of whole database. So either if you are perfect at writing save method and error handling or don’t try save method and try to implement these functionalities either in forms, views, models, etc.
Django Overriding the Save Method Explanation
Illustration of overriding the save method using an Example. Consider a project named
geeksforgeeks having an app named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into
models.py file of geeks app. We will be using CharField for experimenting for all field options. We will override the save method to fill up the SlugField automatically.
Let us explain what happens in above code. save() method from its parent class is to be overridden so we use super keyword. slugify is a function that converts any string into a slug. so we are converting the title to form a slug basically. Let us try to create an instance with “Gfg is the best website”.
Let us check what we have created in admin interface.
Advanced concepts with overriding the save methd
As defined in the starting of this article it is often not recommended to override the save method. Let us check why?
The above code recreates the slug every time the save method is used or if any change is done to the model.
The second reason is if one needs to change the title only but not slug since slug is redirecting to a particular link and is ranking on some search engine. A great issue would be created in a production server. This makes the use of this method of validation unfortunately incorrect. There can be multiple ways to solve above problem, one can declare slug as read-only field and then before making any changes to slug in overridden method we can check if it is empty. This may resolve the problem. So as recommended until you are able to handle errors in save method, don’t override it.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- get_object_or_404 method in Django Models
- Django Models | Set - 1
- Django Models | Set - 2
- Python | Relational fields in Django models
- BigIntegerField - Django Models
- AutoField - Django Models
- BigAutoField - Django Models
- BooleanField - Django Models
- CharField - Django Models
- DateField - Django Models
- DateTimeField - Django Models
- DecimalField - Django Models
- BinaryField - Django Models
- EmailField - Django Models
- DurationField - Django Models
- FileField - Django Models
- FilePathField - Django Models
- FloatField - Django Models
- ImageField - Django Models
- GenericIPAddressField - Django Models
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.