Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def decrease_spacecrafts_weight():
- affected_spacecrafts = (Spacecraft.objects
- .filter(mission__status="Planned", weight__gte=200.0)
- .distinct())
- if not affected_spacecrafts.exists():
- return "No changes in weight."
- num_of_spacecrafts_affected = affected_spacecrafts.count()
- affected_spacecrafts.update(weight=F('weight') - 200.0)
- avg_weight = Spacecraft.objects.aggregate(avg_weight=Avg('weight'))['avg_weight']
- if avg_weight is None:
- avg_weight = 0.0
- avg_weight = round(avg_weight, 1)
- return (f"The weight of {num_of_spacecrafts_affected} spacecrafts has been decreased. "
- f"The new average weight of all spacecrafts is {avg_weight}kg.")
Advertisement
Add Comment
Please, Sign In to add comment