"Django Signal Issue: Calculating and Updating Montant_Total in Reservation Model with Assurance and Options

14 Views Asked by At

I'm using django 4.2

this is my models

class Assurance(models.Model):
    nom = models.CharField(max_length=100)
    cout = models.DecimalField(max_digits=10, decimal_places=2)
    ordre = models.IntegerField(null=True, blank=True)

    def __str__(self):
        return self.nom

class Option(models.Model):
    nom = models.CharField(max_length=255)
    description = models.CharField(max_length=255, null=True, blank=True)
    prix = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.nom

class Reservation(models.Model):

    ETAT_CHOICES = [
        ('en_attente', 'En attente de paiement'),
        ('paye', 'Paiement réussi'),
        ('annule', 'Réservation annulée'),
    ]

    utilisateur = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    voiture = models.ForeignKey(Voiture, on_delete=models.CASCADE)
    date_debut = models.DateField()
    date_fin = models.DateField()
    montant_total = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, editable=False)
    assurance = models.ForeignKey(Assurance, on_delete=models.PROTECT, null=True, blank=True)
    options = models.ManyToManyField(Option, blank=True)
    ID_reservation = models.CharField(max_length=20, null=True, blank=True, editable=False, unique=True)
    date = models.DateTimeField(auto_now_add=True)
    etat = models.CharField(max_length=10, choices=ETAT_CHOICES, default='en_attente')
    ref_reservation = models.CharField(max_length=200, unique=True, blank=True, null=True, editable=False)

this is my signal.py

@receiver(pre_save, sender=Reservation)
@receiver(m2m_changed, sender=Reservation.options.through)
def update_montant_total(sender, instance, action=None , **kwargs):
    # Calculate the difference in days between the start and end date
    duree_reservation = (instance.date_fin - instance.date_debut).days + 1

    # Calculate the total amount for the reservation based on duration and assurance cost
    montant_total = duree_reservation * instance.voiture.prix_journalier

    if instance.assurance:
        montant_total += instance.assurance.cout

    if action == 'post_add':
        # Calculate the total cost of selected options
        montant_option = instance.options.aggregate(total_price=Sum('prix'))['total_price'] or 0
        montant_total += montant_option


    if action == 'post_remove' :
        montant_option = instance.options.aggregate(total_price=Sum('prix'))['total_price'] or 0
        montant_total += montant_option


    # Update the montant_total field of the reservation instance

    instance.montant_total = montant_total
    print("last price",instance.montant_total)

I'd like to calculate my montant_total in my Reservation database and add option price from options model and assurance prix if assurance is selected.

It's work for the assurance but not for the options, I see in the console the price changing but not in the database.

Someone know how i can solve it ?

Thanks !

0

There are 0 best solutions below