I have the datetime filled with auto_now=True
Database is mysql.
Range queries won't work for me directly.
Is there any one liner for this?
For simplicity, how can I get 10 rows with highest values for a field(let it be Integerfield) ?
I have the datetime filled with auto_now=True
Database is mysql.
Range queries won't work for me directly.
Is there any one liner for this?
For simplicity, how can I get 10 rows with highest values for a field(let it be Integerfield) ?
order by the field in descending order and limit to 10?
ORDER BY fieldname DESC LIMIT 10
For model Model
with integer field field
, this should work:
top10 = Model.objects.order_by('-field')[:10]
If you want django to have an index on the field, you need to define it in the model with db_index option:
class Model(models.Model):
field = IntegerField(db_index=True)
Regular database index should work even if your asking for the highest values.