标签:with ini city ref ble app lower some comment
The name that will be used by default for the relation from a related object back to this one. The default is <model_name>_set. (默认值是model_name_set,model_name小写)
This option also sets related_query_name
As the reverse name for a field should be unique, be careful if you intend to subclass your model. To work around name collisions, part of the name should contain ‘%(app_label)s‘ and ‘%(model_name)s‘, which are replaced respectively by the name of the application the model is in, and the name of the model, both lowercased. See the paragraph on related names for abstract models.
from django.db import models class Foo(models.Model): pass class Bar(models.Model): foo = models.ForeignKey(Foo) class Meta: default_related_name = ‘bars‘
The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model). See the related objects documentation for a full explanation and example. Note that you must set this value when defining relations on abstract models; and when you do so some special syntax is available.
If you’d prefer Django not to create a backwards relation, set related_name to ‘+‘ or end it with ‘+‘. For example, this will ensure that the User model won’t have a backwards relation to this model:
user = models.ForeignKey( User, on_delete=models.CASCADE, related_name=‘+‘, )
The name to use for the reverse filter name from the target model. It defaults to the value of related_name or default_related_name if set, otherwise it defaults to the name of the model:
# Declare the ForeignKey with related_query_name class Tag(models.Model): article = models.ForeignKey( Article, on_delete=models.CASCADE, related_name="tags", related_query_name="tag", ) name = models.CharField(max_length=255) # That‘s now the name of the reverse filter Article.objects.filter(tag__name="important")
Like related_name, related_query_name supports app label and class interpolation via some special syntax.
1) default_related_name在class Meta中设置, related_name和related_query_name在外键字段中作为属性设置
2) 反向查询可通过对象进行查询,也可通过filter或values进行查询
3)通过对象查询时,使用related_name;如果related_name未设置,使用default_related_name
4)通过filter或values进行查询时,使用related_query_name
related_query_name的默认值是 related_name(如果设置(自定义)了related_name),否则
related_query_name的默认值是default_related_name(如果设置(自定义)了default_related_name),否则
related_query_name的默认值是model_name
验证如下:
class Publish(models.Model): nid=models.AutoField(primary_key=True) name=models.CharField(max_length=32) city=models.CharField(max_length=32) email=models.EmailField() class Book(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) keepNum=models.IntegerField() commentNum=models.IntegerField() publish=models.ForeignKey(to="Publish", to_field=‘nid‘, related_name=‘pub‘, related_query_name=‘pubs‘) authors=models.ManyToManyField(to=‘Author‘)
publish_obj = models.Publish.objects.get(name=‘人民出版社‘) print(publish_obj.pub.values("pk",‘title‘,‘price‘))
print(models.Publish.objects.filter(name=‘人民出版社‘).values("pubs__pk",‘pubs__title‘,‘pubs__price‘)) print(models.Publish.objects.filter(pubs__pk__gte=1).values(‘pubs__pk‘,‘pubs__title‘,‘pubs__price‘))
class Publish(models.Model): nid=models.AutoField(primary_key=True) name=models.CharField(max_length=32) city=models.CharField(max_length=32) email=models.EmailField() class Book(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) keepNum=models.IntegerField() commentNum=models.IntegerField() publish=models.ForeignKey(to="Publish", to_field=‘nid‘) authors=models.ManyToManyField(to=‘Author‘) class Meta: default_related_name=‘bo‘
publish_obj = models.Publish.objects.get(name=‘人民出版社‘) print(publish_obj.bo.values("pk",‘title‘,‘price‘)) print(models.Publish.objects.filter(name=‘人民出版社‘).values("bo__pk",‘bo__title‘,‘bo__price‘)) print(models.Publish.objects.filter(bo__pk__gte=1).values(‘bo__pk‘,‘bo__title‘,‘bo__price‘))
related_query_name与related_name和default_related_name的联系与区别
标签:with ini city ref ble app lower some comment
原文地址:http://www.cnblogs.com/NewTaul/p/7629859.html