[Answered ]-Specifying order_by on Related Field in Django

2👍

I’m a little late to the party here, but hopefully I can help some future explorer.

I had a very similar problem, and I even found an old bug report that more or less relates to this here, and judging by that, it’s not going to be solved anytime soon; and if the last lines on that page are correct, it will never be solved. So, you simply can’t order by a related field and not retrieve duplicates; whether it be the way you tried through the RelatedManger or some other approach: for instance SomeModel.objects.order_by('related__related_field')

I did find a way to make it work, though it’s not elegant. In my case, I have a model that describes a base model of guitar that I build, and there are photos related to that model. I’d like to have the query order the photos based on a priority level; this allows me to control the order photos are displayed on the website served by the Django back-end. So the ordering is hard-coded into the model like so:

class PhotosModel(models.Model):
    class Meta:
        ordering = ['priority_level']
    priority_level = models.SmallIntegerField()
    photo_src = models.URLField(null=True, blank=True, default=None, max_length=65)
    photo_name = models.CharField(max_length=20)
    photo_description = models.TextField(max_length=200, blank=True, null=True)
    base_model = models.ForeignKey(BaseModelModel,
                                   on_delete=models.CASCADE,
                                   related_name='photos')

    def __str__(self):
        return self.photo_name 

For me, this works well enough, though it does have the obvious downside that it’s always ordered that way. I’d love to know a better way, if anyone has one.

👤Hildy

Leave a comment