Re: compute model field by related field

2019-04-24 Thread omar ahmed
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()

Re: compute model field by related field

2019-04-24 Thread Derek
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:

Re: compute model field by related field

2019-04-23 Thread omar ahmed
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

Re: compute model field by related field

2019-04-23 Thread Derek
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

Re: compute model field by related field

2019-04-22 Thread omar ahmed
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

Re: compute model field by related field

2019-04-20 Thread Derek
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'

Re: compute model field by related field

2019-04-18 Thread omar ahmed
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 >

Re: compute model field by related field

2019-04-17 Thread Derek
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

Re: compute model field by related field

2019-04-16 Thread Makori Breens
Implement rest framework On Tue, Apr 16, 2019, 9:20 PM 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

compute model field by related field

2019-04-16 Thread omar ahmed
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 =