ok Derek i used save() instead of return and it works now 
thank you very much 
@receiver(post_save, sender=Match)
def update_club_score(instance, sender, **kwargs):
# club local won
if instance.score_local > instance.score_visitor:
instance.club_local.won += 1
instance.club_local.save()
instance.club_visitor.lost += 1
instance.club_visitor.save()
# club local lost
if instance.score_local < instance.score_visitor:
instance.club_local.lost += 1
instance.club_local.save()
instance.club_visitor.won += 1
instance.club_visitor.save()
# draw
if instance.score_local == instance.score_visitor:
instance.club_local.draw += 1
instance.club_local.save()
instance.club_visitor.draw += 1
instance.club_visitor.save()


On Wednesday, April 24, 2019 at 8:13:05 AM UTC+2, Derek wrote:
>
> Did you add the required decorator before this function?  (The function is 
> not part of the class; its a standalone appearing in the same module.)
>
> Also, you generally don't put returns in the post_save function: but you 
> do need to call save() on the model instance.
>
> Another example here:
>
> https://stackoverflow.com/questions/13014411/django-post-save-signal-implementation
>
> On Tuesday, 23 April 2019 16:31:43 UTC+2, omar ahmed wrote:
>>
>> yes i studied this tutorial before and i added this function to "Club" 
>> class (( Receiver function class )) but it does not affect fields 
>> def teampoints( sender, instance, **kwargs):
>> if instance.score_local > instance.score_visitor:
>> return instance.club_local.won +1 and instance.club_visitor.lost + 1
>> elif instance.score_local < instance.score_visitor:
>> return instance.club_local.lost + 1 and instance.club_visitor.won + 1
>> else:
>> return instance.club_local.draw + 1 and instance.club_visitor.draw + 1
>>
>>
>> On Tuesday, April 23, 2019 at 3:27:28 PM UTC+2, Derek wrote:
>>>
>>> You need to use Django signals to provide a custom "post_save".  There 
>>> is a good tutorial here:
>>>
>>> https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html
>>>
>>>
>>> On Monday, 22 April 2019 16:22:19 UTC+2, omar ahmed wrote:
>>>>
>>>> i put this function in "Match" class :
>>>> def points(self):
>>>> if self.score_local > self.score_visitor:
>>>> return (self.club_local.won)+1 and (self.club_visitor.lost)+1
>>>> elif self.score_local < self.score_visitor:
>>>> return (self.club_local.lost)+1 and (self.club_visitor.won)+1
>>>> else:
>>>> return (self.club_local.draw)+1 and (self.club_visitor.draw)+1
>>>> and i want it to update fields and 'CalcPoints' function in "Club" 
>>>> class 
>>>> class Club(models.Model):
>>>> ...
>>>> def CalcPoints(self):
>>>> return self.won*3 + self.draw
>>>>
>>>> but until now it does not update objects (( how can i use post_save 
>>>> here )) 
>>>>
>>>> On Saturday, April 20, 2019 at 9:09:01 AM UTC+2, Derek wrote:
>>>>>
>>>>> That should just require a basic if/then logic test; "get" the correct 
>>>>> Club object, update the win/loss field and save the Club. Repeat for both 
>>>>> Clubs.
>>>>>
>>>>> On Thursday, 18 April 2019 14:09:41 UTC+2, omar ahmed wrote:
>>>>>>
>>>>>> thank you for response , derek
>>>>>> but how can i increment 'win' 'lost' or 'draw' Club fields by 
>>>>>> 'winner' Match field
>>>>>>
>>>>>> On Wednesday, April 17, 2019 at 3:26:22 PM UTC+2, Derek wrote:
>>>>>>>
>>>>>>> 1. Add a "winner" field to your Match
>>>>>>> 2. Implement a post_save  signal for the Match model that updates 
>>>>>>> the "won" or "lost" fields for each Club in the match (simple if/then 
>>>>>>> logic 
>>>>>>> based on winner).
>>>>>>>
>>>>>>> PS I think the default values for "won" and "lost" for a Club should 
>>>>>>> be "0" and not "1".
>>>>>>>
>>>>>>> On Tuesday, 16 April 2019 20:19:34 UTC+2, omar ahmed wrote:
>>>>>>>>
>>>>>>>> hello ... i have two models "Club" class and "Match" class and it 
>>>>>>>> has foreign key to Club
>>>>>>>> now i want to increment "won" field (or draw or lost) in "Club" 
>>>>>>>> class by "score_local" and "score_visitor" in "Match" class ..
>>>>>>>> how can i do this
>>>>>>>> class Club(models.Model):
>>>>>>>> name = models.CharField(max_length=100)
>>>>>>>> won = models.IntegerField(default=1)
>>>>>>>> draw = models.IntegerField(default=1)
>>>>>>>> lost = models.IntegerField()
>>>>>>>> goal_for = models.IntegerField()
>>>>>>>> goal_against = models.IntegerField()
>>>>>>>>
>>>>>>>>
>>>>>>>> class Match(models.Model):
>>>>>>>> play_date = models.DateTimeField('play date')
>>>>>>>> club_visitor = models.ForeignKey(Club, on_delete=models.CASCADE, 
>>>>>>>> related_name='match_club_visitor')
>>>>>>>> club_local = models.ForeignKey(Club, on_delete=models.CASCADE, 
>>>>>>>> related_name='match_club_local')
>>>>>>>> score_visitor = models.IntegerField()
>>>>>>>> score_local = models.IntegerField()
>>>>>>>>
>>>>>>>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a9b92a46-a012-46e9-8717-c1e91e07870c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to