简体   繁体   中英

Backward lookup in order_by

Here is example model:

class Sample(models.Model):
    group = models.ForeignKey(SampleSet)
    type = models.IntegerField()
    code = models.IntegerField()

class SampleSet(models.Model):
    some_text = models.TextField()

Is there any ways to use backward lookups in order_by? Need to write method returned SampleSet queryset sorted by laboratory field in Sample with type=0

Something like:

SampleSet.objects.order_by('sample(type=0)__code')

怎么样:

SampleSet.objects.filter(sample__type=0).order_by('sample__code')

You need to filter first before you order

SampleSet.objects.filter(sample__type=0).order_by('sample__code')

If you still need the all samples as well then I'd suspect you need to order by type first, then by code.

SampleSet.objects.order_by('sample__type', 'sample__code')

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