I have such Django models:
class Car(models.Model):
rating = models.PositiveIntegerField(
default=0,
verbose_name=_('Rating'),
)
class ReportInfo(models.Model):
car = models.ForeignKey(
Car,
related_name='car_info',
verbose_name='Report',
)
And I need to form rating for my car instances including info from reports, in such way:
def save(self, *args, **kwargs):
rating = 0
for item in self.car_info.all():
rating += 10
self.rating = rating
super(Car, self).save(*args, **kwargs)
So, I need to get all reports of my car, then get some other data from the report and then save rating into the field. But, self.car_info.all()
returns me old data. That means next: when I click save button in admin page of a new car, my code in save method does not have access to real reports, as they are not created yet.
Do you understand? What can I do?