Site possibilities: worry about, literal chain and you may parent_link

Site possibilities: worry about, literal chain and you may parent_link

Design matchmaking often have recursive relationships. This might be a common situation in one to many relationship habits having mother-guy matchmaking. For example, a category design might have a grandfather community which in itself is an additional Category design otherwise a man design may have an excellent members of the family community which in in itself are also Individual models. In order to establish these relationship you need to make use of the ‘self’ key phrase to resource a comparable model, just like the found when you look at the checklist 7-25.

Record eight-25 One of many Django model connection with care about-referencing design

No matter if model relationships study sizes generally speaking express its dating by way of design target recommendations (elizabeth.grams. habits.ForeignKey(Menu) ), also, it is valid to utilize exact chain to help you site activities (elizabeth.g. patterns.ForeignKey(‘Menu’) ). This procedure is effective when the model meaning buy does not enables you to source design things which aren’t but really in the range that will be a technique referred to as design ‘lazy-loading’.

The fresh new father or mother_link=Correct choice is a personal option for one to one matchmaking (we.elizabeth the activities.OneToOneField data style of) put whenever inheriting design classes, to simply help suggest the kid category job is used because the a relationship to the latest moms and dad category.

Reverse matchmaking: related_name, related_query_identity and symmetric

If you use matchmaking model research systems, Django immediately establishes the opposite matchmaking ranging from studies versions towards the newest _lay reference. That it system try depicted during the list eight-twenty six.

List 7-twenty-six One of many Django model relationship with opposite relationships references

As you can plainly see into the listing eight-26, there are two main routes anywhere between an effective Django dating. The fresh new direct channel relates to using the model toward relationships meaning, in such a case, Product gets most of the Items suggestions which have a dish Break fast such as for instance. To achieve this, make use of Item and you may filter out towards eating plan ForeignKey resource (e.grams. Items.items.filter(menu=breakfast) ).

But it’s also possible to use a Menu instance (e.g. breakfast in listing 7-26) and get all Item records with a menu instance, this is called a reverse relationship or path. As you can see in the listing 7-26, the reverse relationship uses the ._set syntax (e.g. breakfast.item_set.all() to get all Item records with a the breakfast instance).Now that you know what a reverse relationship is, let’s explore the options associated with this term.

The fresh related_identity solution makes you customize the name otherwise disable an excellent opposite design matchmaking. Renaming an opposite relationships provides a great deal more user-friendly sentence structure over the _lay sentence structure out-of list eight-twenty six, while disabling an other relationships is effective whenever a related model is utilized various other contexts and you may blocking accessibility good contrary matchmaking is needed to own entry to explanations.

Instance, into the list eight-26 the opposite relationships uses the fresh breakfast.item_lay.all() sentence structure, but if you replace the community in order to activities.ForeignKey(. related_name=’menus’) , you can make use of the reverse matchmaking morning meal.menus.all() sentence structure. In order to disable an opposing matchmaking you can utilize the new + (along with indication) to the associated_term value (e.g. habits.ForeignKey(. related_name=’+’) ).

Number seven-27 That many Django model connection with contrary relationship queries

Notice the way the Menu query within the number eight-27 spends the object wie findet man ein MГ¤dchen mit einem FuГџfetisch mention of the filter the Menu facts through their Product relationship. By default, opposite relationships issues utilize the label of your own design, thus in cases like this, the newest associated Diet plan design try Item , therefore the ask profession was product . Yet not, for folks who define the newest relevant_identity solution to your an industry so it well worth requires precedence. Including, that have patterns.ForeignKey(. related_name=’menus’) the reverse inquire within the list 7-27 gets Menu.stuff.filter(menus__price__gt=1) , which takes me to the associated_query_title alternative.

The fresh new related_query_term choice is accustomed override new related_title solution really worth for cases where you desire the opposite ask to own another type of profession value. Such as, having models.ForeignKey(. related_name=’menus’,related_query_name=’onlyitemswith’) the opposite relationship source for menus was checklist 7-twenty six carry out continue to work, but the opposite relationships ask away from record eight-twenty-seven do switch to Diet plan.objects.filter(onlyitemswith__price__gt=1) .

Dejen comentarios

Su dirección de correo electrónico no será publicada.*



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Volver arriba