1. 程式人生 > >related_query_name與related_name和default_related_name的聯系與區別

related_query_name與related_name和default_related_name的聯系與區別

default create plan nbsp ocs with 反向 when support

default_related_name

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

related_name

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=+,
)

related_query_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的聯系與區別