Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
django: ordenar filas según el número de coincidencias de columnas

Tengo un modelo:-

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

para un perfil de usuario (digamos: ('maestros', 'India', 'estudiante') quiero filtrar todos los perfiles de usuario ordenados por la cantidad de campos que coinciden con el perfil de usuario dado, es decir, primero los 3 campos que coinciden con los perfiles luego 2 campos que coincidan con los perfiles y así sucesivamente. ¿Alguien puede sugerir una manera de hacer esto de manera eficiente?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede lograr esto usando expresiones condicionales .

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!