44
You can’t do that because that property is not in MySQL, but in your python code. If you really want to do this, you can on the client-side(though it will be very slow):
sorted(Thing.objects.all(), key=lambda t: t.name)
4
order_by
happens on the sql level, so it can’t make use of properties, only field data.
have a look at the queryset api, you may be able to make use of e.g. extra
to annotate your query and sort on that
- [Django]-How to use 2 different cache backends in Django?
- [Django]-Setting up a foreign key to an abstract base class with Django
- [Django]-Django REST framework: non-model serializer
4
Have a look at django-denorm. It lets you maintain calculated values in the database (which you can then use to sort efficiently) for the cost of a single method decorator.
- [Django]-How to use custom AdminSite class?
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Steps to Troubleshoot "django.db.utils.ProgrammingError: permission denied for relation django_migrations"
3
You can only order by database fields. You can pull all records, turn them into a list, and sort them, although that may be inefficient and slow.
Or you can add a database field called name
and fill it in via a post-save hook; then you can sort it using order_by
.
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-Multiple images per Model
- [Django]-What is the equivalent of django.db.models.loading.get_model() in Django 1.9?