Tried to update field X with a model instance, < X >. Use a value compatible with CharField.

686 views
Skip to first unread message

hajar

unread,
May 12, 2020, 1:43:43 AM5/12/20
to Django users
hello django users , 

I am trying to update a class using signals , you will understand once you see the code below 

class mail_item_count(models.Model):
   country = models.CharField(max_length=100)
   count = models.IntegerField(default=1)
   

    def __str__(self):
       return '{}, {}'.format(self.country,self.count)

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


@receiver(post_save, sender=mail_item)
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()

but I am getting this error : 

" Tried to update field dashboard.mail_item_count.country with a model instance, <Pays: Maroc >. Use a value compatible with CharField. "

can you help me out solving it 

Uri Kogan

unread,
May 12, 2020, 2:08:40 AM5/12/20
to Django users
I think, for that you'll have to show your "mail_item" model too.

hajar

unread,
May 12, 2020, 2:41:16 AM5/12/20
to Django users

class mail_item(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')

Uri Kogan

unread,
May 12, 2020, 3:32:04 AM5/12/20
to Django users
From the error message it can be seen that mail_item_count has "country" item which is "CharField".
But... the signal receiver sets this to instance of  "Pays" object which assumes it is "ForeignKey".

hajar

unread,
May 12, 2020, 3:53:57 AM5/12/20
to Django users
aht do you advice me to do ?

Uri Kogan

unread,
May 12, 2020, 4:21:03 AM5/12/20
to Django users
I would suggest changing your mail_item_count model like this:

class mail_item_count(models.Model):
    country = models.ForeignKey(Pays, on_delete=models.CASCADE)
    count = models.IntegerField(default=1)

Motaz Hejaze

unread,
May 12, 2020, 5:01:24 AM5/12/20
to Django users
In signal access the model of country through the foreignkey Pays.origin.country_name

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/01889167-1fdb-43f4-b1ed-24021eb00989%40googlegroups.com.

hajar

unread,
May 12, 2020, 6:36:52 AM5/12/20
to Django users
can you explain to me more please 
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Motaz Hejaze

unread,
May 12, 2020, 7:17:55 AM5/12/20
to Django users
Access the country model through the foreign key in maiĺ_item , so you can get the country name and store it in mail_item_count

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/29dd9a0d-e7d9-4843-af78-da94ea0b65c3%40googlegroups.com.

hajar Benjat

unread,
May 12, 2020, 7:35:47 AM5/12/20
to django...@googlegroups.com
I think I am already doing this!

Can you check my mail_item class please 

Reply all
Reply to author
Forward
0 new messages