JohnB
JohnB

Reputation: 77

Linking two django fields in two different models

I am going slightly crazy as I am sure this is a super simple question. I have two models in django:

class Play(models.Model):
    gid = models.IntegerField()
    pid = models.IntegerField(primary_key=True)
    off = models.CharField(max_length=5)
    deff = models.CharField(max_length=5)
    type = models.CharField(max_length=10)

class FGXP(models.Model):
    pid = models.IntegerField()
    fgxp = models.CharField(max_length=5)
    fkicker = models.CharField(max_length=10)

This is a football database and when there is a Play that scores there is another table where additional data about the extra point is held: FGXP. So in Play pid is the primary_key. In FGXP there is a link between pid and the pid in play. In my Django model should I link them? I will end up needing to join the data when I do a query.

Thanks - an apologies if a duplicate.

Upvotes: 0

Views: 63

Answers (1)

Jason
Jason

Reputation: 11363

You can do a Foreign Key from FGXP to Play like so

class FGXP(models.Model:
   play = models.ForeignKey(Play, on_delete= models.CASCADE)
   fgxp = models.CharField(...)
   ...

which would make FGXP exist on a Play instance as the attribute fgxp_set

I think a FK would work here because there are potentially more than one extra point plays, especially when a penalty is called.

Upvotes: 2

Related Questions