The initial Django model for the record 7-twenty-four are Goods that’s much like the that presented within the checklist 7-twenty two, except the brand new variation when you look at the number 7-24 gets the even more unhealthy calories and rates sphere. Next, inside list seven-twenty four is the Drink model that has the object job, you to by itself has the activities.OneToOneField(Amenity,on_delete=habits.CASCADE,primary_key=True) meaning.
The fresh designs. Another disagreement to the_delete=patterns.CASCADE tells Django if the connection list try deleted (i.elizabeth. the thing ) their most other list (we.age. the fresh Drink ) also be deleted, that it history disagreement prevents orphaned data. Finally, the primary_key=Genuine informs Django to accesso incontri per stringere amicizie utilize the connection id (we.age. ) since no. 1 key in lieu of using a different sort of and you may standard line id , a strategy rendering it more straightforward to track dating.
Also the database level advantages of performing a one to 1 matchmaking (e.g. increased studies maintenance), Django habits have an enthusiastic API to express the newest accessibility off investigation associated with this type of relationships, that is said in the next chapter towards CRUD information across Django model dating.
Choices for relationship design research types
In past times your explored Django analysis models and their many options so you can tailor how they handle data, such as for instance : restricting values, allowing empty and you can null beliefs, setting up preset philosophy and you can implementing DDL guidelines. In this section you will then see regarding the options available to possess Django dating model studies sizes.
Data integrity choice: on_delete
Most of the design relationships manage dependencies anywhere between both, very a significant behavior so you’re able to explain is really what happens to the latest most other party when one-party is completely removed. New towards the_delete option is readily available for this goal, to see which regarding information on the other side away from a love whenever that side is removed.
For example, in the event that a product design features a meal ForeignKey() community leading so you’re able to a recipe model (i.e. for example checklist seven-twenty two, a one to a lot of relationships: an item constantly end up in you to definitely Diet plan , and you may a menu has many Factors ), what happens so you can Item model records in the event the its associated Selection model such was erased ? Will be Goods model records plus removed ?
- on_delete=designs.CASCADE (Default).- Automatically deletes related suggestions when the associated such as for instance is removed (e.grams. should your Diet plan Morning meal including is actually removed, all the Product info referencing the fresh new Selection Morning meal instance are also erased)
- on_delete=patterns.Include .- Inhibits an associated such out-of are removed (elizabeth.g. in case your selection community into Goods uses ForeignKey(Selection,on_delete=patterns.PROTECT) , people you will need to beat Diet plan circumstances referenced by the Items circumstances is actually blocked).
- on_delete=habits.SET_NULL .- Assigns NULL so you can relevant suggestions when the relevant for example is taken away, mention this calls for the field so you’re able to additionally use the latest null=Genuine option (age.g. whether your Selection Breakfast like try removed, the Goods records referencing new Eating plan Breakfast such as try assigned NULL on the menu job really worth).
- on_delete=models.SET_Standard .- Assigns a default worthy of to relevant info in the event that related for example is removed, note this involves industry so you’re able to additionally use a standard option value (elizabeth.grams. when your Diet plan Breakfast particularly is removed, all Goods info referencing the new Eating plan Morning meal instance is actually tasked an effective default Menu such as for instance on their menu community really worth).
- on_delete=habits.Put .- Assigns an admiration put as a result of good callable so you’re able to associated suggestions when the fresh new related such as is completely removed (elizabeth.g. if for example the Diet plan Morning meal eg is actually erased, most of the Items details referencing the latest Menu Morning meal eg try tasked an for example on their eating plan profession worth place because of a beneficial callable function).
- on_delete=models.DO_Absolutely nothing .- No step is taken whenever relevant records was eliminated. It is fundamentally an adverse relational database practice, thus automagically, databases can establish an error due to the fact you might be making orphaned facts having zero really worth, null or else. If you use it value, you need to make sure the databases dining table doesn’t demand referential integrity.
Нет Ответов