I have 2 models Parent
, Child
class Parent(models.Model):
id = Base64UUIDField(primary_key=True, editable=False)
cost = models.DateTimeField(default=None, blank=True, null=True)
class Child(models.Model):
id = Base64UUIDField(primary_key=True, editable=False)
cost = models.DateTimeField(default=None, blank=True, null=True)
parent = models.ForeignKey(Parent, related_name= "children", related_query_name= "child")
I need to populate cost
column of Parent
objects to maximum cost of all children of that parent
I have tried to annotate to a new column new_cost
, and its works.
parents.annotate(new_cost=Max('child__cost'))
But I need to populate values to existing column cost
. Tried something like this, but not working.
parents.update(cost=Max('child__cost'))