Reference alternatives: self, exact strings and you may parent_hook

Design dating sometimes keeps recursive dating. This really is a common condition in one single to a lot of relationships activities with father or mother-guy relationships. For example, a category design have a pops industry that alone is yet another Class design or a man design can have an excellent family profession that itself are other Individual activities. In order to identify such relationships you ought to make use of the ‘self’ keyword to help you resource a comparable design, as revealed into the listing 7-twenty five.

Number seven-twenty five That of a lot Django design connection with mind-referencing model

Whether or not design relationships data systems usually display their dating as a result of model object recommendations (age.g. habits.ForeignKey(Menu) ), furthermore legitimate to use exact chain in order to source habits (elizabeth.g. patterns.ForeignKey(‘Menu’) ). This method is beneficial if the design meaning acquisition cannot enables you to reference design objects which aren’t yet when you look at the extent that’s a strategy also known as model ‘lazy-loading’.

The newest parent_link=Correct option is a private choice for one-to-one dating (we.age brand new activities.OneToOneField study sorts of) used when inheriting design categories, to assist mean the kid class industry should be put since the a relationship to this new parent group.

Opposite matchmaking: related_identity, related_query_term and you can shaped

If you utilize matchmaking design analysis brands, Django instantly sets the opposite relationship ranging from studies brands into the the latest _lay site. This apparatus try illustrated for the record seven-twenty-six.

Record seven-twenty six That of a lot Django model reference to opposite relationships references

As you care able to see for the listing eight-twenty six, there are 2 paths anywhere between a Django matchmaking. This new head channel concerns utilizing the design towards the relationships definition, in this instance, Item becomes most of the Items facts having a menu Morning meal such as. To do this, you employ Goods and you may filter out with the diet plan ForeignKey reference (e.grams. Item.things.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 new associated_term alternative enables you to customize the identity or disable a good reverse design relationship. Renaming a contrary relationship provides more user friendly sentence structure along side _set syntax from number seven-twenty-six, where as disabling an opposing matchmaking is beneficial whenever a related model can be used in other contexts and you will clogging the means to access a beneficial contrary relationships is required getting use of explanations.

Such as, within the number seven-twenty-six the reverse matchmaking uses the fresh break fast.item_put.all() sentence structure, but if you change the field so you’re able to habits.ForeignKey(. related_name=’menus’) , you should use the opposite relationship break fast.menus.all() sentence structure. So you’re able to eliminate an opposite relationship you can utilize the newest + (and signal) with the related_title really worth (e.grams. models.ForeignKey(. related_name=’+’) ).

Number seven-twenty seven That of numerous Django model experience of reverse dating requests

Observe how the Menu query when you look at the list eight-twenty seven uses the item mention of the filter out the Diet plan ideas through the Item relationship. By default, opposite dating question make use of the term of your design, therefore in this instance, the brand new related Menu design was Product , as well as the ask career are items . not, if you define the newest relevant_term solution with the a field this worth takes precedence. Such as, having designs.ForeignKey(. related_name=’menus’) the reverse query into the checklist eight-27 becomes Selection.things.filter(menus__price__gt=1) , all of which requires me to the latest associated_query_label alternative.

The fresh new associated_query_label option is regularly override the new associated_title alternative worth to have cases where you cena skout want the opposite ask to have a different sort of industry really worth. Including, having habits.ForeignKey(. related_name=’menus’,related_query_name=’onlyitemswith’) the opposite relationship reference to possess menus is list eight-twenty six do continue to work, nevertheless reverse matchmaking inquire away from listing seven-twenty-seven do change to Diet plan.items.filter(onlyitemswith__price__gt=1) .


No responses yet

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *