2

For some reason order_by() is not working for me on a queryset. I've tried everything I can think of, but my Django/MySQL installation doesn't seem to be doing anything with order_by() method. The list appears to just remain in a fairly unordered state, or is ordered on some basis I cannot see.

My Django installation is 1.8.

An example of one of my models is as follows:

class PositiveTinyIntegerField(models.PositiveSmallIntegerField):
    def db_type(self, connection):
        if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
            return "tinyint unsigned"
        else:
            return super(PositiveTinyIntegerField, self).db_type(connection)

class School(models.Model):
    school_type = models.CharField(max_length=40)
    order = PositiveTinyIntegerField(default=1)

# Make the identity of db rows clear in admin
def __str__(self):
    return self.school_type

And here is the the relevant line from my view:

schools = School.objects.order_by('order')

At first I thought the problem was related to having used the non-standard PositiveTinyIntegerField() defined by a class I found on a website somewhere which allows me to use the MySQL Tiny Integer field. However, when I ordered by 'id', or 'school_type' the list still remained in an order that appeared fairly random to my eye.

I could put in my own loop which orders the queryset after it has been retrieved, but I'd really rather solve this issue so I can use the standard Django way of doing it.

I hope someone can see where the issue may be coming from.

Terry Rozmus
  • 396
  • 3
  • 17
  • If `DEBUG = True`, your queries should be logged. Can you show the logged query from `django.db.connection.queries`? – knbk Mar 04 '16 at 12:43
  • 1
    It seems very unlikely that order_by isn't working. It's much more likely that the field in the database has different values than you expect or that you're not running the code you think you're running. – Glenn Mar 04 '16 at 13:03
  • I change the values on the order column myself in admin. Having just rechecked them, I can assure you they are correct. In addition, if I change the view to .filter(id=1), only one item appears on the list, so it is definitely the right code I am dealing with. As for logging. Debug=True is set in the settings file, but I am not getting any SQL sent to any of the logs. The bottom response on this StackOverflow post appears to have a method, but when I added the lines to settings.py and restarted the app, my SQL queries were still not logged. – Terry Rozmus Mar 04 '16 at 13:38
  • Opps, the post referred to is here: http://stackoverflow.com/questions/971667/django-orm-how-to-view-or-log-the-executed-query – Terry Rozmus Mar 04 '16 at 13:39
  • Are you certain you are reloading the server after making changes to the code? If you show the complete view and template, it might show the problem. – Alasdair Mar 04 '16 at 14:09
  • You will definitely need to show the full view and template, plus preferably an example of the output you're getting and an explanation of how that differs to what you expect. – Daniel Roseman Mar 04 '16 at 15:36

1 Answers1

1

I managed to resolved it with some help from the comments here. I tried writing the school object to stdout using sys.write.stdout(str(school)). The logs then showed me that in fact the data was being ordered correctly, so the problem had to be with how the data was being packaged before being rendered by the template.

I wrote the view some time ago before I decided I wanted it ordered, so it turned out the problem was caused by each school object (with an attached tree of related data) being read into a dictionary. Once I changed the data type to the list, the schools then rendered in my intended order.

Terry Rozmus
  • 396
  • 3
  • 17