Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
In Django, how do I get all the instances of a model that have other instances of a related model attached to it with a ForeignKey field?

I am trying to write a query that retrieves all the categories that have at least one post attached to it. In other words, I want to 'exclude' any category that doesn't have any post.

These are my models for Category and Post:

class Category(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique=True)


class Post(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique=True)
    body = models.TextField()
    category = models.ForeignKey(Category, blank=True, default="")

This is the code I am using in my query, currently it fetches all the categories, even if no Post is 'attached' to it:

categories = Category.objects.all()

What I want is something like this:

categories = Category.objects.filter(
    #  Only ones that have at least one Post that has it's 'category' field set to it.
)

I've searched the docs and everywhere else, but I can't figure out a solution.

Please let me know how this can be done.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use following query:

categories = Category.objects.filter(post__isnull=False).distinct()

This will get all the categories where post is not null. Since, one category could have multiple posts, you will get duplicate instances with same id. Take a distinct to remove duplicate categories.

Note, that distinct(*fields) is postgresql specific. If you are using a different database, just use distinct().

about 4 years ago · Santiago Trujillo Denunciar

0

Get all unique category ids by querying Post for distinct category and then filter Category by ids.

id_list = Post.objects.values_list('category_id').distinct()
catgories = Category.objects.filter(id__in=id_list)
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda