I think I am already doing this! Can you check my mail_item class please
On Tue, May 12, 2020, 11:17 AM Motaz Hejaze <trapper...@gmail.com> wrote: > 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 > > On Tue, 12 May 2020, 12:37 pm hajar, <hajarben...@gmail.com> wrote: > >> can you explain to me more please >> >> Le mardi 12 mai 2020 10:01:24 UTC+1, Motaz Hejaze a écrit : >>> >>> In signal access the model of country through the foreignkey >>> Pays.origin.country_name >>> >>> On Tue, 12 May 2020, 10:21 am Uri Kogan, <urk...@gmail.com> wrote: >>> >>>> 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) >>>> >>>> >>>> >>>> On Tuesday, May 12, 2020 at 10:53:57 AM UTC+3, hajar wrote: >>>>> >>>>> aht do you advice me to do ? >>>>> >>>>> Le mardi 12 mai 2020 08:32:04 UTC+1, Uri Kogan a écrit : >>>>>> >>>>>> 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". >>>>>> >>>>>> On Tuesday, May 12, 2020 at 9:41:16 AM UTC+3, hajar wrote: >>>>>>> >>>>>>> >>>>>>> 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') >>>>>>> >>>>>>> >>>>>>> >>>>>>> Le mardi 12 mai 2020 06:43:43 UTC+1, hajar a écrit : >>>>>>>> >>>>>>>> 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 >>>>>>>> >>>>>>> -- >>>> 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...@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 >>>> <https://groups.google.com/d/msgid/django-users/01889167-1fdb-43f4-b1ed-24021eb00989%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> 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+unsubscr...@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 >> <https://groups.google.com/d/msgid/django-users/29dd9a0d-e7d9-4843-af78-da94ea0b65c3%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAHV4E-c5QG-CdDLrDNC4XnBJbeVNYM7T5bFePAZL733m2Jw2Qg%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAHV4E-c5QG-CdDLrDNC4XnBJbeVNYM7T5bFePAZL733m2Jw2Qg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMcj6WcTwgV9e%2BoCwV8Gg5206Zb7OfRSmxCqyVL8%3DbwysgJ9_g%40mail.gmail.com.