django中的related_name和related_query_name是什么? [英] what is related_name and related_query_name in django?

查看:298
本文介绍了django中的related_name和related_query_name是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django框架中的代码与django中的 related_name和related_query_name 有关。请django专家在django中解释related_name,代码如下:

i have an issue with the code in django framework regarding to related_name and related_query_name in django. please django expert explain the related_name in django, the code is below:

related_name ='+'

related_name='+'

推荐答案

相关名称



Django维护每个对象的后向关系,以便于访问相关对象。假设您有两个名为学校和学生的模型,并且一所学校可以有多个学生。因此,您将具有类似以下的模型定义

Related Name

Django maintains backward relation on each object for easy access to related objects. Suppose you have two models named "School" and "Student" and one school can have multiple students. So you will have model definition something like this

class School(models.Model):
    name = models.CharField(max_length=55)
    city = models.Charfield(max_length=55)

class Student(models.Model):
    name = models.CharField(max_length=55)
    school = models.ForeignKey(School)

现在,如果您有学校物件,则可以

Now if you have an school objects then you can access all students of that school with writing query explictly.

school = School.objects.get(id=1)
# Now if need all students of this school, first thing that come in your mind would be
Student.objects.filter(school=school)
# But instead of this, you can access all students by
school.student_set.all()

此处 student_set 是Django的默认相关名称。但是您可以使用与自定义相关的名称,例如

Here student_set is the default, related name made by Django. But you can have your custom related names like this

class Student(models.Model):
    name = models.CharField(max_length=55)
    school = models.ForeignKey(School, related_name='students')
# Now you can do
school.students.all()



相关名称中的特殊字符



如果定义 related_name ='+',则向后关系将在对象上不可用,并且 school.student_set.all()会给您错误。 / p>

Special Character in related name

If you define related_name='+' then backward relation would not be available on object and school.student_set.all() will give you error.


如果您希望Django不要创建反向关系,请将related_name设置为'+'或以'+'结尾。例如,这将确保User模型不会与此模型具有向后关系:

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:



相关查询名称



related_query_name与related_name相似,但是它在查询集中使用。

Related Query Name

related_query_name is similar to related_name but it gets used in queryset.

如果您需要通过学校对学生应用某些过滤器模型,那么您可以这样做

If you need to apply some filter on student via school model, then you would do

School.objects.filter(student__name='abc')

但是,如果您定义了related_query_name,则可以这样做

But if you define related_query_name then you can do

class Student(models.Model):
    name = models.CharField(max_length=55)
    school = models.ForeignKey(School, related_query_name='abc')
# Now you can do
School.objects.filter(abc__name='abc')

引用文档以便进一步参考: https://docs.djangoproject.com/en/3.0/ ref / models / fields /

Refer doc for further reference: https://docs.djangoproject.com/en/3.0/ref/models/fields/

这篇关于django中的related_name和related_query_name是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆