OneToOneField() definition brings the only to one dating, where the very first disagreement Product means the connection model
OneToOneField() definition brings the only to one dating, where the very first disagreement Product means the connection model

The initial Django design in list seven-twenty-four was Item that's much like the that shown in the number seven-twenty-two, except this new type within the checklist eight-24 comes with the additional unhealthy calories and you will rates fields. Next, in list 7-twenty four is the Take in model that has the thing job, one by itself comes with the activities.OneToOneField(Amenity,on_delete=habits.CASCADE,primary_key=True) meaning.

Brand new models. The next argument towards_delete=models.CASCADE tells Django when the connection number is actually removed (we.e. the item ) their most other listing (i.age. the latest Take in ) additionally be erased, so it past disagreement suppresses orphaned analysis. Finally, the main_key=Genuine tells Django to use the partnership id (we.e. ) since first key in the place of having fun with a unique and you can default column id , a method which makes it simpler to track relationship.

In addition to the database level advantages of doing a single to a single dating (elizabeth.g. increased analysis fix), Django models have an enthusiastic API in order to explain this new access off research associated with this type of dating, that's informed me next chapter towards CRUD facts across the Django design matchmaking.

Choices for dating model study items

Before your looked Django data sizes in addition to their many options to modify the way they deal with data, such : restricting philosophy, making it possible for empty and you will null viewpoints, installing preset opinions and you may enforcing DDL statutes. Contained in this section you will learn in regards to the options available to own Django dating model study versions.

Analysis stability choices: on_erase

The model matchmaking do dependencies anywhere between one another, very a significant choices so you can determine is exactly what goes wrong with the latest most other cluster when one party is removed. Brand new on_erase choice is available for which mission, to determine what regarding ideas on the other hand out-of a love when that top is completely removed.

Such as, if something design provides a menu ForeignKey() career directing so you can a menu design (we.e. particularly record 7-twenty two, a one to numerous matchmaking: a product or service constantly belong to you to Diet plan , and a dish has many Things ), what will happen so you can Product design information in the event that the associated Diet plan design like is erased ? May be the Product design info plus deleted ?

  • on_delete=habits.CASCADE (Default).- Instantly deletes related info in the event the relevant such is removed (e.g. if your Eating plan Morning meal particularly was erased, all of the Goods facts referencing brand new Diet plan Breakfast such as are also erased)
  • on_delete=patterns.Manage .- Inhibits a related for example of becoming removed (elizabeth.g. in case the eating plan job on Item uses ForeignKey(Diet plan,on_delete=models.PROTECT) , one make an effort to lose Selection days referenced by Goods hours is blocked).
  • on_delete=habits.SET_NULL .- Assigns NULL in order to associated facts in the event the relevant such as for instance is removed, mention this requires the field so you can additionally use new null=True solution (e.g. whether your Eating plan Break fast for example is removed, most of the Items information referencing the brand new Eating plan Morning meal like is actually tasked NULL on the diet plan industry worth).
  • on_delete=activities.SET_Default .- Assigns a standard value in order to associated records when the associated including is completely removed, note this calls for the field to help you additionally use a standard alternative really worth (elizabeth.grams. in case the Eating plan Break fast particularly are erased, all Items records referencing the fresh new Menu Morning meal such as for example is actually assigned an excellent default Selection such as for instance on their eating plan occupation really worth).
  • on_delete=designs.Set .- Assigns an esteem lay thanks to an effective callable so you're able to related info when the fresh related such as is completely removed (age.grams. whether your Menu Morning meal instance was removed, most of the Items records referencing the latest Eating plan Morning meal like are tasked an enthusiastic including to their diet plan industry value put using a good callable means).
  • on_delete=models.DO_Little .- No action is actually removed when associated information was removed. This will be essentially a detrimental relational databases habit, very by default, database can establish an error due to the fact you happen to be making orphaned suggestions with no value, null or else. When you use so it really worth, you should guarantee the database dining table doesn't enforce referential stability.

Vélemény, hozzászólás?

Az e-mail-címet nem tesszük közzé.