1👍
Not sure if your models are correct. With a ForeignKey from Points to Card, that means each Card has multiple Points instances. So when you have a Card object, you can’t access the points directly: card.points
is a queryset of all the Points objects that are related to this card.
Either you need to determine somehow which related Points is the one you mean, or change your models. Probably using a OneToOne field instead of a ForeignKey would be more appropriate: then you do only have a single Points instance, and card.points
will refer to that instance as you expect and you will then be able to access its nb_current_points
value.
Edit
The recursion error is because you have attached your handler to the save signal for all classes, not just ScanEvent. You should be sure to specify the sender
parameter when registering it:
signals.post_save.connect(add_points_to_card, sender=ScanEvent)
Note that you’ll need to move those out of the ScanEvent class otherwise you’ll get name errors.
1👍
In your models, a Card may have several Points associated. That is why instance.scanned_card.points is a RelatedManager, not a Point.
Try this:
def add_points_to_card(sender, instance, **kwargs):
for points in instance.scanned_card.points.all():
points.nb_current_points += instance.won_points