Esmail Mahjoor
Esmail Mahjoor

Reputation: 142

django using an Integerfield for sorting

I have a model which has an IntegerField. this field may change after creation of object. I wanna use it for the "trend" part in my project, like those objects with the larger number must shown at top. so what is the best way (efficient) to do this. my current idea is:

class Map(models.Model):
    rank = models.IntegerField()

and later use it like this:

sorted_list = Map.objects.order_by("-rank")[:50]

is this a good idea even if there are many requests for the trend page?

Upvotes: 1

Views: 332

Answers (1)

Tim Nyborg
Tim Nyborg

Reputation: 1649

Yes, this is efficient because the database is doing the sorting work. Just ensure you have an index on the rank column, or it will get very slow as row count increases.

If there are going to be many hits, cache the result with an appropriate timeout.

Upvotes: 2

Related Questions