Pergunta

I have the following query set on Django in 2 places the only thing is the limitation and gives me different results

Here is the first result for the following QuerySet

list_empleados = empleado.objects.filter(
    empresa=session_empresa
).order_by('-puntos')[:3]

enter image description here

The proper result should be:

19,13,9 instead of 9,19,13 see what i mean?

Here is the second QuerySet

list_empleados = empleado.objects.filter(
    empresa=session_empresa
).order_by('puntos')

and here is the result

enter image description here

The proper result should be:

19,13,9,1 instead of 1,13,19,9 see what i mean?

models.py

class empleado(models.Model):


    empresa = models.ForeignKey(empresa)
    nombre = models.CharField(max_length=50)

    fecha_nacimiento = models.DateField(auto_now_add=False)

    GENDER_CHOICES = (

        ('M', 'Masculino'),
        ('F', 'Femenino'),
    )

    sexo = models.CharField(max_length=1, choices=GENDER_CHOICES)

    avatar = StdImageField(upload_to='avatar/%Y/%m/%d', variations={
        'large': (300, 300, True),
        'medium': (50, 50, True),
        'thumbnail': (98, 122, True)})

    correo = models.EmailField(max_length=100)

    departamento = models.ForeignKey(departamento)

    telefono = models.CharField(max_length=21)
    direccion = models.CharField(max_length=200)
    twitter = models.CharField(max_length=15)
    usuario = models.CharField(max_length=15)
    password = models.CharField(max_length=40)
    primer_lugar = models.CharField(max_length=20)
    segundo_lugar = models.CharField(max_length=20)
    tercer_lugar = models.CharField(max_length=20)
    goleador = models.CharField(max_length=20)
    puntos = models.CharField(max_length=2, default=0)
    partidos = models.CharField(max_length=4, default=0)

    def avatarEmpleado(self):
        return '<img src="/media/%s" height="90" width="90">' % (self.avatar.thumbnail)

    avatarEmpleado.allow_tags = True

    def __unicode__(self):
        return self.nombre
Foi útil?

Solução

It's because puntos is a charfield so it's ordering lexicographically, so "9" > "19". If you want to order it numerically you need a integerfield or floatfield.

I would suggest to edit the model but if you can't check out this solution

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top