You are reading the article Complete Guide To Django On_Delete updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Complete Guide To Django On_DeleteIntroduction to Django on_delete
When handling foreign key concepts, you need to declare them as parameters in the on-delete arguments of the foreign keys. When using Django’s delete argument, specific arguments determine the impact on the primary key table caused by the deletion of a referenced foreign key value. These arguments determine the appropriate actions when deleting the parent value or referenced object. Options like this offer a great extent of flexibility to database-oriented operations.Syntax of Django on_delete
Given below is the syntax of Django on_delete:Field Name = models.ForeignKey(WASD, on_delete = OPERATION TYPE)
In this context, the leftmost value represents the field that we create in reference. We use this field to establish the relationship in the background, acting as the referential field or the field responsible for pulling data from the parent field. It is important to mention the field that will be referenced further in the framework. In database terms, the field given on the left end will act as the field inheriting the value of the foreignkey field.
Next, the models.Foreignkey is the function that mentions that this operation is a foreign key creation operation. This function call is a mandatory item to use. It says that a foreign key is expected to be created further in this section. The syntax means the foreign key function is called and operated from the model’s program. Next to these function names, the arguments have to be placed. From a Django perspective, this function call is predefined. Let’s discuss the arguments used further.
After the models.Foreignkey function calls the arguments of the function that need to be mentioned. This is where the on_delete method comes into play. First, we will inherit the argument “WASD” as the foreign key, and we will also utilize the “on_delete” function. The on_delete function will equal it to the various options associated with it.on_delete Options
Below are the multiple options for the on_delete arguments:1. CASCADE
Setting the on_delete argument to cascade significantly impacts the referred objects if you delete the referenced object. This implies that if you delete the referenced object from the database, all entries for that object will be deleted from the entire database. This is how the CASCADE option works in the background. So, there are some impacts on data integrity as far as the CASCADE options are concerned.2. PROTECT
PROTECT is the direct opposite of the cascade option; In this case, when there is an impact on the actual object, all instances of the data on the referenced object are not deleted. So, no deletion happens. This ensures that we protect the data on the referenced object before initiating the deletion process.3. RESTRICT
The RESTRICT is similar to the PROTECT option and does the same job, just like the deletion option. The only difference here is when the deletion is targeted on the referenced object, then the ON_DELETE will raise an error called the RestrictedError. But the RESTRICT will allow deletion to happen when the referencing object and the object which is referenced object is allotted concerning a different common object, then the deletion is permitted.4. SET_NULL
The option functions the same way the name suggests; When a deletion occurs on the referenced object, the referencing object’s value will update to NULL. Consequently, the referencing object will be assigned a NULL value. This is how the SET NULL will operate.5. SET_DEFAULT Create a Django on_delete argument
Given below shows the creation of Django on_delete Argument:
Changes in chúng tôi file: We made the following changes to the chúng tôi file, and these changes will be reflected in the front end of the form used.
In this case, we utilize two different on_delete arguments: RESTRICT and CASCADE options.
chúng tôifrom chúng tôi import models from django.contrib.auth.models import User # Model variables # Create your models here. class Bride(models.Model): models.CharField(max_length=200,null=True) ….. Example_Post_Graduation_college = models.CharField(max_length=400,null=True) Example_Rasi = models.CharField(max_length=200,null=True) Example_Nakshatra = models.ForeignKey(User, null=True, on_delete=models.RESTRICT) Example_Creator = models.ForeignKey(User, null=True, on_delete=models.CASCADE) def __str__(self): return self.name
Due to the use of the RESTRICT parameter, the creator’s deletion of the record Karthi does not result in the record being placed on the user table.Conclusion
Django application developers can benefit from using flexible options like on_delete to delete records in their database. This feature provides greater control over the impact of deletions on referenced records, making it a valuable tool for managing data in Django.Recommended Articles
We hope that this EDUCBA information on “Django on_delete” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Complete Guide To Django On_Delete
Update the detailed information about Complete Guide To Django On_Delete on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!