简体   繁体   中英

Django custom order_by

I've got a model eg. Car with a foreign key eg. Owner, which may or may not be blank. The Car has a creation_date.

I would like to order these cars by date, but if the car has an owner, the date of birth of the owner must be taken instead of the creation_date of the car.

Is this possible?

Have a look at this similar question: Good ways to sort a queryset? - Django

You can't use the model's Meta ordering as it only accepts one field

https://docs.djangoproject.com/en/dev/ref/models/options/#ordering

You can't use the query order_by('creation_date', 'birthdate') as it only sorts by birthdate if they have the same creation_date

So, you could write a custom manager to do incorporate a custom sort for you.

import operator
class CarManager(models.Manager):
    def get_query_set(self):
        auths = super(CarManager, self).get_query_set().all().order_by('-creation')
        ordered = sorted(auths, key=operator.attrgetter('birthday'))
        return ordered

class Car(models.Model):
    sorted = CarManager()

so now you can query:

Car.sorted.all()

to get all a queryset of sorted car's

This is possible by falling back to SQL:

Car.objects.filter(...).extra(select={'odate': '''
  if(owner_id,
     (select date_of_birth from owner_table where id=owner_id),
     creation_date
  )
'''}).order_by('odate')

if function is MySQL-specific. In case of SQLite or Postgres you should use case statement.

您可以编写一个返回相应日期的方法(如果汽车有所有者返回生日,则返回creation_date),然后根据此方法订购模型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM