Question

I'm designing a Django application for athletes to register for a competition. There are several weight classes available to each athlete and each weight class is stored in a foreign key in another model. The models are as follows:

class Tournament(models.Model):
    tourney_name = models.CharField(max_length=30)
    tourney_date = models.DateTimeField()
    tourney_classes = models.ManyToManyField(WeightClass, related_name='tourney_name', blank=True)

class Athlete(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email_address = models.EmailField(unique=True)
    athlete_class = models.ForeignKey(WeightClass, related_name='entries')

class WeightClass(models.Model):
    class_name = models.CharField(max_length=100, unique=True)
    class_info = models.CharField(max_length=50)
    class_group = models.CharField(max_length=100)

The weight classes are separated into groups and I want the classes to be grouped on the registration form. For example we have Junior Men as a group. Within the Junior Men's group we have Lightweight, Middleweight, and Heavyweight. Then we have Junior Women, Senior Men, etc. I want the form to be option buttons for each weight class grouped separately.

  • Junior Men
    • Lightweight
    • Middleweight
    • Heavyweight
  • Junior Women
    • Lightweight
    • Middleweight
    • Heavyweight
  • etc.

So I need my form to query the database for a tournament, and then retrieve all of the weight classes for that particular tournament, and then display the weight classes as shown above. How do I do this?

Was it helpful?

Solution

I'd suggest you have class_group as a ForeignKey relationship within WeightClass. Hence something like:

class WeightClass(models.Model):
    class_name = models.CharField(max_length=100, unique=True)
    class_info = models.CharField(max_length=50)
    class_group = models.ForeignKey(WeightGroup)

class WeightGroup(models.Model):
    name = models.CharField(max_length=100, unique=True)

You could then first display all weight groups, and then filter WeightClass based on the user selection.

If the Lightweight instance should be the same for both Junior Men and Junior Women then you should maybe consider to model it as a many to many relationship.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top