Django model linking with another model to create a form

I am trying to develop a scoring sheet app for my club.

I have models for Player, Scoresheet, Season ,etc.

What I would like to achieve is basically when a game takes place, the scoresheet model will have a form where the person can capture the total score of the team and also the goals of each player.

My issue is how do I link the players stats to the scoresheet in a form, for example the player model has goals scored, freekicks taken, red cards, yellow cards. I want the scoresheet model form to be able to fill out that stats for each player.

I am aware that something is missing or I am doing something wrong. I am not sure what it is.

my Models are as follows

class Player(models.Model):
    user = models.OneToOneField(User,on_delete=models.SET_NULL,null=True,related_name="playeruser")
    Player_name = models.CharField(max_length=40)
    Player_surname = models.CharField(max_length=40)
    Player_ID = models.CharField(max_length=40)
    Player_Email = models.CharField(max_length=40)
    Player_Contact = models.CharField(max_length=40)
    category = models.ForeignKey(Category,on_delete=models.CASCADE,related_name="pcategory")
    Player_preferred = models.CharField(choices=Roles,default="default",max_length=16)
    
    def __str__(self):
        return self.Player_name + " " + self.Player_surname


class Score_sheet(models.Model):
    season = models.ForeignKey(Season,on_delete=models.CASCADE,related_name="season")
    category = models.ForeignKey(Category,on_delete=models.CASCADE,related_name="sscategory")
    players = models.ManyToManyField(Player,related_name="players")
    captain = models.ForeignKey(Player,on_delete=models.CASCADE,related_name="captain")
    Date = models.DateField()
    Venue= models.CharField(max_length=40,null=True,blank=True)
    description = models.TextField(null=True,blank=True) 
    goals= models.IntegerField(null=True,blank=True)
    freekicks = models.IntegerField(null=True,blank=True)
    redcards = models.IntegerField(null=True,blank=True)
    yellowcards = models.IntegerField(null=True,blank=True)

    def __str__(self):
        return str(self.season) + " " + self.Venue

Use django forms, you van create the forms you want. docs

Back to Top