我们从一个简单的事说起:
吃货君到水果摊捡漏买了两种水果,分别是人参果和蟠桃;Later, the fruit vendor had the feeling that he had lost an important treasure.,It seems to be the same as a100It's about an old grandpa.,Look over your collection bill.,The heart tightens when it sees the bill from the foodie guy,He's the one who bought my baby.,I'm gonna find out what he bought my baby.! The shopkeeper found the fruit he bought through the name of the food guy,After discovering that there were 10,000 year old peaches and 100 million year old ginseng.,Bawl your eyes out.!!!
What kind of luck did Eat-kun have?????
Below we define two models based on the above mini-plot, a buyer model as well as a fruit model, with one buyer corresponding to multiple fruit models, a simple one-to-many model:
class Buyer(): name = (verbose_name='Buyer's name', max_length=10) Alipay_id = (verbose_name='Alipay account') age = (verbose_name='Age of buyer',blank = True) class Fruit(): buyer = (Buyer, related_name='buyer_fruit') fruit_name = (verbose_name='Fruit name', max_length=10) weight = (verbose_name='Fruit weight')
Usually, we want to query the buyer to buy which fruit, first of all, according to the conditions to find the buyer information, and then according to the buyer information to find the buyer to buy the fruit, in this case as follows:
# First get the objects in the table pointed to by the foreign keys in the fruit model: buyer = (age = 100).first() # Then get the data in the child table via the '_set' method: fruits = buyer.fruit_set.all() """ django By default every object in the primary table has an attribute that is a foreign key,It can be used to query information about all the sub-tables belonging to the master table。 The name of this attribute defaults to the name of the child table in lowercase plus the_set()to indicate,Here our main table isbuyer,The word list isfruit,So the attribute of the foreign key of the primary table isfruit_set """
The above fruit_set is the attribute of the foreign key that django creates by default for the object buyer, I personally recommend using a customized way to define the foreign key of the main table, so that the use of more familiar with some of the bar!
And related_name to realize this function, in the field table in the definition of the foreign key, add related_name field to specify this field table in the main table corresponding to the foreign key attribute.
Below:
class Fruit(): buyer = (Buyer, related_name='buyer_fruit') """ main table:buyer subsheet:fruit subsheet在main table中对应的外键属性:related_name='buyer_fruit' """
Then we can find the information we need by customizing the foreign key:
# First get the objects in the table pointed to by the foreign keys in the fruit model: buyer = (age = 100).first() # Then get all the information about the sub-table through the customized foreign key in the sub-table: fruits = buyer.buyer_fruit.all()
Above this django in related_name usage description is all I share with you, I hope to give you a reference, and I hope you support me more.