Dynamic Radio Form

9 views
Skip to first unread message

J.T.

unread,
May 4, 2020, 6:24:54 PM5/4/20
to Django users
I'm working on an app that will allow the user to select the winner between two teams (radio buttons) and I need the info saved to the database. I'm having trouble with the radio forms part. I've read the documentation and searched Google all day, but I can't seem to wrap my heads around it. 

Here are my models:

class Schedule(models.Model):
    LEAGUE_CHOICES = (('HS','HS'), ('NFL''NFL'), ('NCAA','NCAA'))
    week = models.IntegerField()
    game_id = models.IntegerField(unique=True)
    away_team = models.CharField(max_length=55)
    home_team = models.CharField(max_length=55)
    away_id = models.IntegerField(unique=True)
    home_id = models.IntegerField(unique=True)
    league = models.CharField(max_length=15choices=LEAGUE_CHOICES)
    def __str__(self):
        return f'Week {self.week} {self.away_team} vs {self.home_team}'

class Selection(models.Model):
    username = models.ForeignKey(User, on_delete=models.CASCADE)
    week = models.ForeignKey(Schedule, on_delete=models.CASCADE)
    select_one = models.CharField(max_length=50)
    select_two = models.CharField(max_length=50)
    select_three = models.CharField(max_length=50)
    select_four = models.CharField(max_length=50)
    select_five = models.CharField(max_length=50)
    select_six = models.CharField(max_length=50)
    select_seven = models.CharField(max_length=50)
    select_eight = models.CharField(max_length=50)
    select_nine = models.CharField(max_length=50)
    select_ten = models.CharField(max_length=50)
    tie_breaker = models.IntegerField()
    def __str__(self):
        return f'Week {self.week} selections for {self.username}'

Below is a portion of what I want the template to look like when rendered. It takes the teams from the "Schedule" model
and displays them.
I then want the id of the selection (the value) of each game saved to the "Selections" model (select_one, select_two, etc.)
I can't figure out how to tie in the view so it saves the data to the db.
I realize I need to create a forms.py, import it into the view, but that is the part I'm having trouble understanding. I don't
know what my forms.py should look like since the team names will change weekly and each match-up is it's own radio selection.
Any help is greatly appreciated. Also, if I'm screwing this up on the models level, let me know. I realize that could
also be an issue.
JT

1 Oklahoma Oklahoma StateNCAA
1 Texas Christian HoustonNCAA
1 Dallas PhiladelphiaNFL
1 Houston IndianapolisNFL
1 New Orleans AtlantaNFL





Chetan Ganji

unread,
May 5, 2020, 12:12:30 PM5/5/20
to django...@googlegroups.com
To answer your question, you could add an extra field on schedule model to store the winner 
e.g. winner = models.CharField(max_length=55)

IMO, league and team in the schedule model should be separate tables. You would use fk to them in schedule model. 
winner should also be an fk to team.


Regards,
Chetan Ganji
+91-900-483-4183


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/398cb0ad-fb81-49ff-bbee-53aa054b6a17%40googlegroups.com.

Jason Turner

unread,
May 5, 2020, 2:04:38 PM5/5/20
to django...@googlegroups.com
Thanks. That makes sense. Appreciate your help. 

Reply all
Reply to author
Forward
0 new messages