I want to have a model with calculated fields that I can apply sorting on. For example, let\'s say that I have the following model:
class Foo(models.Model):
I find that without the *args and **kwargs in the save method, it returns an error. And as celopes stated, this is only a solution if you don't mind materializing the computed field in the database.
class Foo(models.Model):
A = models.IntegerField(..)
B = models.IntegerField(..)
C = models.ForeignKey(..)
D = models.IntegerField(..)
E = models.IntegerField(..)
def save(self, *args, **kwargs):
self.D = self.A - self.B
self.E = self.A - self.C.X
super(Foo, self).save(*args, **kwargs)
class Meta:
ordering = ["E", "D"]