0
votes

I am having strange issue with django ordering. For some reason I am not able to order my List View by multiple fields.

Here is my class.

class Syslog(models.Model):

    receivedat = models.DateTimeField(default=timezone.now)
    facility = models.PositiveSmallIntegerField()
    priority = models.PositiveSmallIntegerField()
    fromHost = models.CharField(max_length=50)
    message = models.TextField(max_length=500)

    class Meta:
           ordering = ['id','receivedat', 'fromHost']

Here is my view class as well:

class HomePageView(ListView):
    model = Syslog
    template_name = 'home.html'
    context_object_name = 'all_logs'
    paginate_by = 10
    ordering = ['-id', 'receivedat', 'fromHost']

When a single field in ordering is specified (e.g. ordering = ['-id']), but when I add a second or third parameter everything goes back to defaults (only the first field of ordering is getting recognized)

Does anyone have encounter a similar problem?

The same problem persists in the Django shell as well. When I execute the following query Syslog.objects.all().order_by('id') everything looks good, but when I add another field to order_by (e.g. Syslog.objects.all().order_by('id','fromHost')) the second field doesn't get recognized at all.

Regards, Jordan

2

2 Answers

3
votes

Intsead of this -> Syslog.objects.all().order_by('id','fromHost')

Use this -> Syslog.objects.order_by('id','fromHost')

remove .all()

0
votes

After slept on my problem I have figured out why I was not getting the result I expected. The issue is that my development data set is quite similar and while the ordering changes it didn't reflect on the template because of objects similarity.