Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1K
Visualizações
what is related_name and related_query_name in django?

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='+'

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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()

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()

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.

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_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')

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')

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda