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

207
Vistas
django: sort rows based on number of column matching

I have a model:-

class Userprofile(models.Model):
    user=models.OneToOneField(settings.AUTH_USER_MODEL)
    education=models.models.CharField(max_length=20,blank=True,null=True)
    country=models.CharField(max_length=20,blank=True,null=True)
    occupation=models.CharField(max_length=20,blank=True,null=True)     ....

for a user profile (let's say: ('masters','India','student') I want to filter all the user profiles ordered by the number of fields it matches with the given user profile i.e first all 3 fields matching profiles then any 2 fields matching profiles and so on.Can anyone suggest a way to do this efficiently?

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

0

You can achieve this using conditional expressions.

from django.db.models import Value, Case, When, IntegerField, F

education, country, occupation = 'masters','India','student'
Userprofile.objects.annotate(education_matched=Case(
    When(education=education, then=Value(1)), 
    default=Value(0), 
    output_field=IntegerField()
), country_matched=Case(
    When(country=country, then=Value(1)), 
    default=Value(0), 
    output_field=IntegerField()
), occupation_matched=Case(
    When(occupation=occupation, then=Value(1)), 
    default=Value(0), 
    output_field=IntegerField()
)).
annotate(matched=F('education_matched') + F('country_matched') + F('occupation_matched')).
order_by('matched')
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