Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (120 points)

hello guys 

I am using django and I created 2 models the main one and the other one will take 2 columns from the main one using signals , the mail_item_count have 2 columns country and the count of how many this country repeated 

but I don't know what is the problem with the line

count_object.count = count_object.count + 1

this is my models.py

class mail_items(models.Model):
    #mail_item_fid = models.OneToOneField(Mail_item_Event,on_delete=models.CASCADE)
    Event_cd = models.OneToOneField(Mail_item_Event,on_delete=models.CASCADE,related_name="mail_item_event_cd")
    office_Evt_cd = models.ForeignKey(Office,on_delete=models.CASCADE, related_name='office_Ev')

    Date_Evt = models.DateTimeField()
    Pays_origine = models.ForeignKey(Pays, on_delete=models.CASCADE ,related_name='paysOrigine')
    Pays_destination = models.ForeignKey(Pays,on_delete=models.CASCADE,related_name='paysDestination')
    Expediteur_id = models.ForeignKey(Client,on_delete=models.CASCADE,related_name='expedi')
    Destinateur_id = models.ForeignKey(Client,on_delete=models.CASCADE,related_name='destin')

class mail_item_count(models.Model):
    country = models.CharField(max_length=100)
    mail_items_count = models.IntegerField(null=True)

    def sum():
        a = sum(instance.count for instance in mail_item_count.objects.all())    
        return a 

@receiver(post_save, sender=mail_items)

def update_count(sender, instance, created, **kwargs):

    count_object = mail_item_count.objects.get_or_create(country=instance.Pays_origine)
    count_object.count = count_object.count + 1
    count_object.save()

Please log in or register to answer this question.

Browse Categories

...