How to order the django administrator exit

0

I have to sort the records that are loaded from the django administrator, by item-> brand-> model, but I sort by the id not by the name.

models.py

class ElementoModelo(models.Model):
    elemento = models.ForeignKey(Elemento)
    marca = models.ForeignKey(Marca)
    modelo = models.CharField(max_length=50, unique=True)

class Meta:
    ordering = ['elemento', 'marca', 'modelo']

def __str__(self):
    return ("%s - %s - %s"%(self.elemento, self.marca, self.modelo )).strip() or "-"

admin.py

class ElementoModeloAdmin(admin.ModelAdmin):
    list_display = ('elemento', 'marca', 'modelo',)

They could guide me Greetings

    
asked by Alberto 13.04.2017 в 17:42
source

1 answer

0

I would overwrite the default behavior of the get_queryset method of ModelAdmin :

class ElementoModeloAdmin(admin.ModelAdmin):
    list_display = ('elemento', 'marca', 'modelo', )

    def get_queryset(self, request):
        return super(ElementoModeloAdmin, self).get_queryset(request).order_by('elemento', 'marca', 'modelo', )

I hope it helps you:).

Greetings!

    
answered by 01.06.2017 в 15:28