Hi all
Going to be hard to try and explain this, but here goes:
I have a Model called Subscription and a Model Wheels.
class Subscription(models.Model):
wheels = models.ManyToManyField(Wheels, blank=True)
price = models.IntegerField(default=0)
class Wheels(models.Model):
(not actually empty of course)
That model Subscription has a ManyToMany relationship variable called wheels, which points to the Wheels Model. And a price variable that shows the cost of the number of wheels in the subscription.
Now I want to do a override save (or post/pre save, all three have the same behavior) where it calculates the wheels in the subscription * 10.
Save method below is part of Subscription
def save(self, *args, **kwargs):
# Automatically calculate cost of subscription
wheels_price = self.wheels.count() * 10
self.price = wheels_price
print(self.wheels.count())
# Save to original Model
super(Subscription, self).save(*args, **kwargs)
Now I save this using the admin GUI, clicking a ‘wheels’ and hitting save. I’ve did a count in the save as well, to see where it went wrong. Only if I save it twice, it will show the correct amount of wheels and calculate the price. There’s no change on the first save.
However, if I check the database after the first save, I do see that the Wheels foreignkey is populated, but the save says it is not.
{
"amount_paid": 0,
"price": 0,
"wheels": [
2
],
"get_wheels_count": 1
}
Any clues?