omar ahmed
omar ahmed

Reputation: 663

how can i make relation between two fields with different django classes?

i have two models "Club" class and "Match" class and it has foreign key to Club but i can not 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):
    league_names = models.ForeignKey(League, on_delete= models.CASCADE, related_name='club')
    name = models.CharField(max_length=100)
    logo = models.ImageField(upload_to='media/core', max_length=255, null=True, blank=True)
    year_of_establishment = models.IntegerField(default=1900)
    won = models.IntegerField(default=0)
    draw = models.IntegerField(default=0)
    lost = models.IntegerField(default=0)

    def CalcPoints(self):
         return self.won*3 + self.draw            

    total_points = property(CalcPoints)

class Match(models.Model):
    play_date = models.DateTimeField('play date')
    occasion = models.ForeignKey(League, on_delete=models.CASCADE, related_name='match')
    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()

Upvotes: 1

Views: 1109

Answers (2)

Raydel Miranda
Raydel Miranda

Reputation: 14360

You can't establish a relation between to fields even on the same model, but you can override the save method in Match you could implement a signal handler to be called after every match get saved.

@receiver(post_save, sender=Match)
def update_club_score(sender, instance, **kwargs):
    # 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()
    # Local lost.
    if instance.score_local < instance.score_visitor:
        instance.club_visitor.won += 1
        instance.club_visitor.save()
        instance.club_local.lost += 1
        instance.club_local.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()

Upvotes: 2

VnC
VnC

Reputation: 2016

You will need to update the fields of your models somehow.

I would suggest something like:

First, write a function that gets you the clubs from the database:

def get_clubs(local_club_id, visitor_club_id):
    local_club = Club.objects.filter(pk=self.local_club_id)[0]
    visitor_club = Club.objects.filter(pk=self.visitor_club_id)[0]
    return local_club, visitor_club

And then you can update the wins and losts appropriately (in your views.py):

def update_scores(request):
    match = request.match
    club_local, club_visitor = get_clubs(match.club_local.pk, match.club_visitor.pk)
    if match.score_local > match.score_visitor:
        # home team won
        club_local.update(won=match.club_local.won + 1)
        club_visitor.update(lost=match.club_visitor.lost + 1)
    elif match.score_visitor > match.score_local:
        # away team won
        club_visitor.update(won=match.club_visitor.won + 1)
        club_local.update(lost=match.club_local.lost + 1)
    else:
        # it's a draw
        club_local.update(draw=match.club_local.draw + 1)
        club_visitor.update(draw=match.club_visitor.draw + 1)

After that your property function should work as expected.

Upvotes: 0

Related Questions