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

135
Vistas
Get object with the highest number of related objects

Is it possible to get object which has the highest number of related objects?

I want to choose most used plan which is a plan with the maximal number of UserPlan objects.

I can get the number but not the instance.

number_of_users = Plan.objects.aggregate(max_users=Max('userplan'))['max_users']

code:

class UserPlan(Model):
   plan = ForeignKey('Plan'..)

class Plan(Model):
   ...

    @staticmethod
    def favorite(self):
        number_of_users = Plan.objects.aggregate(max_users=Max('userplan'))['max_users']
        # ?

I could find it using loop but it could be slow.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Since the information is already stored in a column, how about we just sort it?

obj_most_popular = Plan.objects.order_by('userplan').first()

You can read more about it in the docs here: https://docs.djangoproject.com/en/1.10/ref/models/querysets/#order-by

over 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