1

I have such models:

class Configuration(models.Model):
    @property
    def latest_value_A(self):
        return self.result_set.latest("created_time").data.get("value_A")

class Result(models.Model):
    data = models.JSONField()  # "value_A" key is present in JSON
    configuration = models.ForeignKey(Configuration, on_delete=models.CASCADE)
    created_time = models.DateTimeField()

I want to do something like Configuration.objects.order_by("latest_value_A") which obviously fails cause it's just a property and not stored in DB. But what would be the proper way of doing it?

UPDATE: I'm using PostgreSQL

1 Answers1

0

You can use the sorted function from python. Something like:

sorted(Configuration.objects.all(), key=lambda t: t.latest_value_A)

A reference if needed: Ordering Django queryset by a @property

Patrick Freitas
  • 681
  • 1
  • 5
  • 18