Use QuerySet.extra()
along with CASE ... END
to define a new field, and sort on that.
Stops.objects.extra(select={'cost': 'CASE WHEN price=0 THEN 0 '
'WHEN type=:EXPRESS_STOP THEN price/2 WHEN type=:LOCAL_STOP THEN price*2'},
order_by=['cost'])
That, or cast the QuerySet
returned from the rest to a list, then use L.sort(key=operator.attrgetter('cost'))
on it.
Related Contents:
- django template system, calling a function inside a model
- Django: Display Choice Value
- matching query does not exist Error in Django
- Django dynamic model fields
- What is a “slug” in Django?
- Django set default form values
- What’s the difference between select_related and prefetch_related in Django ORM?
- How to access a dictionary element in a Django template?
- Programmatically saving image to Django ImageField
- Django FileField with upload_to determined at runtime
- Performing a getattr() style lookup in a django template
- Django, creating a custom 500/404 error page
- django html template can’t find static css and js files
- Django TemplateDoesNotExist?
- Django rest framework serializing many to many field
- How do I create a slug in Django?
- Why doesn’t django’s model.save() call full_clean()?
- Django post_save() signal implementation
- Django Aggregation: Summation of Multiplication of two fields
- How to create an object for a Django model with a many to many field?
- How do I restrict foreign keys choices to related objects only in django
- How to get the domain name of my site within a Django template?
- Get all related Django model objects
- Writing a __init__ function to be used in django model
- Django Rest Framework and JSONField
- django.db.migrations.exceptions.InconsistentMigrationHistory
- How to store a dictionary on a Django Model?
- Using a Django custom model method property in order_by()
- How to change the file name of an uploaded file in Django?
- Is this the way to validate Django model fields?
- Nested inlines in the Django admin?
- How to have a Python script for a Django app that accesses models without using the manage.py shell?
- Django Unique Together (with foreign keys)
- LEFT JOIN Django ORM
- Any way to make {% extends ‘…’ %} conditional? – Django
- How to make Django slugify work properly with Unicode strings?
- Django: Why do some model fields clash with each other?
- How do I perform query filtering in django templates
- How do I make an auto increment integer field in Django?
- Django Blob Model Field
- Split models.py into several files
- Django admin file upload with current model id
- How to access the user profile in a Django template?
- Access request in django custom template tags
- Django: How to create a model dynamically just for testing
- Django admin – inline inlines (or, three model editing at once)
- Override a form in Django admin
- “{% extends %}” vs “{% include %}” in Django Templates
- What is the best way to access stored procedures in Django’s ORM
- QuerySet, Object has no attribute id – Django