What kind of queryset can I do, to order type of strategy in conservative, moderate and risky?

0

This is my class:

class FirFondoDistribucionAdmin(admin.ModelAdmin):
    """
    Administrador de las distribuciones de dichos fondos
    """
    list_display = ('fondo', 'tipo_distribucion', 'porcentaje', 'is_activo', 'fecha')


admin.site.register(FirFondoDistribucion, FirFondoDistribucionAdmin)

This is my model:

class FirFondoDistribucion(models.Model):
    fondo = models.ForeignKey(FirFondo, null=False, blank=False, help_text='Llave foranea de Fondo', related_name='fir_fondo_distribucion')
    tipo_distribucion = models.CharField('Tipo de la estrategia', max_length='5', choices=FIR_TIPO_DISTRIBUCION)
    porcentaje = models.FloatField('Porcentaje', max_length=4, null=False, blank=False)
    is_activo = models.BooleanField(default=False)
    fecha = models.DateTimeField('Fecha Creacion', auto_now_add=True, auto_now=True, blank=False, null=False)

class FirFondoDistribucion(models.Model):
fondo = models.ForeignKey(FirFondo, null=False, blank=False, help_text='Llave foranea de Fondo', related_name='fir_fondo_distribucion')
FIR_TIPO_DISTRIBUCION = (
(0, 'Conservador'),
(1, 'Moderado'),
(2, 'Arriesgado'),)
tipo_distribucion = models.CharField('Tipo de la estrategia', max_length='5', choices=FIR_TIPO_DISTRIBUCION)
porcentaje = models.FloatField('Porcentaje', max_length=4, null=False, blank=False)
is_activo = models.BooleanField(default=False)
fecha = models.DateTimeField('Fecha Creacion', auto_now_add=True, auto_now=True, blank=False, null=False)

So I am my model, I try with everything, I will see the model, in case there is a problem.

    
asked by Mauricio Rivas 03.07.2018 в 18:46
source

1 answer

2

You do not need to do any queryset . You have two options:

  • Use the ordering in your model through class Meta :

    class FirFondoDistribucion(models.Model):
        # ...
    
        class Meta:
            ordering = ['tipo_distribucion']
    
  • Use the ordering of the ModelAdmin :

    class FirFondoDistribucionAdmin(admin.ModelAdmin):
        """
        Administrador de las distribuciones de dichos fondos
        """
        list_display = ('fondo', 'tipo_distribucion', 'porcentaje', 'is_activo', 'fecha')
        ordering = ['tipo_distribucion']
    
  • Option 1 applies to all views while option 2 applies only to the Django admin.

    Update

    Now I understand that what you want is to have that order in specific: Conservador , Moderado and Arriesgado . In that case it depends on how you have defined your choices for FIR_TIPO_DISTRIBUCION .

    To handle it that way you could use:

    FIR_TIPO_DISTRIBUCION = (
        (0, 'Conservador'),
        (1, 'Moderado'),
        (2, 'Arriesgado'),
    )
    

    Since those values are stored in the database as integers, then the order you need will be applied.

        
    answered by 03.07.2018 / 18:58
    source