Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
Custom order field with Django Rest Framework

I have a model like

class MyModel(models.Model):
    COLOR_CODES = ['RED', 'YELLOW', 'GREEN']
    name = models.CharField(db_column='name', max_length=200, blank=False, null=False, unique=True)
    colorCode = EnumField(db_column='color_code', choices=COLOR_CODES, null=False, default='GREEN')
    

    class Meta:
        managed = True
        db_table = 'MyModel'
        ordering = ['colorCode']

I would like to order the quesryset by colorCode, but not as GREEN, RED, YELLOW items. I want to make the order like RED, YELLOW, GREEN items.

Is it possible? Is there something like Java Comparator? Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You could use the Python sorted function, but to take advantage of Django queries:

from django.db.models import Value, IntegerField, When, Case
# Don' t set the default order in the model (this also improves performance), simply write this query when you need this particular order
MyModel.objects.annotate(order=Case(When(colorCode='RED', then=0), When(colorCode='YELLOW', then=1), default=Value(2), output_field=IntegerField())).order_by('order')

Otherwise you could change your model:

from enum import Enum

class MyModel(models.Model):
   class ColorCodes(Enum):
      # In the admin interface you will see 'red' and not '0r' (so don't worry about ugly names), the second value is stored in the database and is used for order the queryset
      red = ('red', '0r')
      yellow = ('yellow', '1y')
      green  = ('green', '2g')
      
      @classmethod
      def get_value(cls, member):
         return cls[member].value[0]
  

   colorCode = CharField(db_column='color_code', null=False, default=ColorCodes.get_value('green'), max_length=2, choices=[x.value for x in ColorCodes])
    
   class Meta:
      managed = True
      db_table = 'MyModel'
      ordering = ['colorCode']
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda