lundi 2 mai 2016

Reference multiple foreign keys in Django Model

I'm making a program that helps log missions in a game. In each of these missions I would like to be able to select a number of astronauts that will go along with it out of the astronauts table. This is fine when I only need one, but how could I approach multiple foreign keys in a field?

I currently use a 'binary' string that specifies which astronauts are to be associated with the mission (1 refers to Jeb, but not Bill, Bob, or Val and 0001 means only Val), with the first digit specifying the astronaut with id 1 and so forth. This works, but it feels quite clunky.

Here's the model.py for the two tables in question.

class astronauts(models.Model):
    name = models.CharField(max_length=200)
    adddate = models.IntegerField(default=0)
    experience = models.IntegerField(default=0)
    career = models.CharField(max_length=9, blank=True, null=True)
    alive = models.BooleanField(default=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Kerbals"

class missions(models.Model):

    # mission details
    programid = models.ForeignKey(programs, on_delete=models.SET("Unknown"))
    missionid = models.IntegerField(default=0)
    status = models.ForeignKey(
        missionstatuses, on_delete=models.SET("Unknown"))
    plan = models.CharField(max_length=1000)

    # launch
    launchdate = models.IntegerField(default=0)
    crewmembers = models.IntegerField(default=0)

    # recovery
    summary = models.CharField(max_length=1000, blank=True)
    recdate = models.IntegerField(default=0)

    def __str__(self):
        return str(self.programid) + '-' + str(self.missionid)

    class Meta:
        verbose_name_plural = "Missions"

I saw a post about an 'intermediate linking table' to store the crew list but that also isn't ideal.

Thanks!

Aucun commentaire:

Enregistrer un commentaire