Resource solutions: care about, literal strings and you may parent_hook

Resource solutions: care about, literal strings and you may parent_hook

Model matchmaking either keeps recursive relationship. This will be 420 sitio de citas a common condition in one to numerous relationships habits that have mother-kid relationships. Like, a course design can have a parent occupation that alone is another Category model or one design may have a friends occupation that in itself are other Individual patterns. In order to determine such relationships you need to utilize the ‘self’ key phrase to site a similar model, just like the revealed into the list seven-twenty five.

Number 7-25 You to of several Django design reference to worry about-referencing model

Even though model relationships studies versions usually share their dating compliment of design target records (age.grams. activities.ForeignKey(Menu) ), also, it is legitimate to utilize exact strings in order to reference patterns (age.grams. activities.ForeignKey(‘Menu’) ). This technique is effective if the design definition buy doesn’t allow you to reference design things which are not yet , into the extent and that’s a method also referred to as model ‘lazy-loading’.

The latest father or mother_link=Correct option is an exclusive option for one to one relationship (i.elizabeth this new models.OneToOneField data types of) utilized when inheriting design kinds, to greatly help imply the little one class community shall be used as the a link to the new moms and dad category.

Opposite dating: related_identity, related_query_identity and you will symmetric

When you use relationships model analysis versions, Django instantly establishes the reverse relationships anywhere between studies systems with the the fresh _lay site. So it apparatus is actually depicted inside number seven-twenty-six.

Number 7-26 That of numerous Django design relationship with opposite relationship records

Perhaps you have realized within the checklist 7-26, there are two main paths anywhere between an effective Django dating. The newest direct route concerns using the design with the matchmaking meaning, in this case, Product gets all Goods information that have a recipe Morning meal such as for example. To achieve this, you use Item and you may filter into menu ForeignKey source (age.g. Items.objects.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 relevant_title alternative allows you to customize the title otherwise eliminate a good opposite model dating. Renaming a contrary relationship brings much more easy to use sentence structure over the _place sentence structure regarding record eight-twenty six, while disabling an opposing matchmaking is helpful whenever a connected model is used various other contexts and you will blocking access to a great opposite dating is necessary getting accessibility reasons.

Like, during the record seven-26 the opposite relationships spends the newest breakfast.item_lay.all() sentence structure, but if you replace the field so you can patterns.ForeignKey(. related_name=’menus’) , you can make use of the reverse relationship morning meal.menus.all() syntax. So you’re able to disable an opposite matchmaking you can make use of the brand new + (along with signal) to your related_title well worth (e.grams. activities.ForeignKey(. related_name=’+’) ).

Checklist 7-twenty-seven You to of numerous Django model relationship with reverse relationship concerns

See the way the Selection ask during the checklist 7-27 uses the item mention of the filter out most of the Selection details thru the Items matchmaking. By default, opposite relationship question use the term of the design, therefore in this instance, the fresh related Eating plan design is Goods , therefore the ask community was goods . Although not, if you establish the new related_label solution toward an area it value requires precedence. Such as for example, that have models.ForeignKey(. related_name=’menus’) the opposite inquire into the checklist eight-twenty seven gets Menu.things.filter(menus__price__gt=1) , which takes me to this new related_query_identity choice.

New related_query_term choice is used to override the new related_title choice worthy of to own instances when you want the reverse inquire having a different sort of industry value. Like, which have activities.ForeignKey(. related_name=’menus’,related_query_name=’onlyitemswith’) the opposite matchmaking source having menus are checklist seven-twenty six create still work, nevertheless reverse matchmaking inquire out of number eight-twenty seven carry out change to Menu.items.filter(onlyitemswith__price__gt=1) .

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *