I’m trying to build a form that displays dynamic choices
How do I build a Form where the athlete’s Sport is a choicefield populated with sports related to the Event that the athlete is participating in…
my models are as follows…
class Sport(models.Model):
Type = models.CharField(max_length=15)
class EventCode(models.Model):
Description = models.CharField(max_length=95)
Sports = models.ManyToManyField(Sport)
class Athlete(models.Model):
Event = models.ForeignKey(EventCode)
Sport = models.ForeignKey(Sport)
I assume you know the event code when you are building the form
This is the way you filter on the
EventCode:lets say the event is jump then you will get [(5,’long jump’),(7,’frog jump’)….]
Now the way you set the related choices is
If you want to do this when you select an
eventin the form then you can pass theeventasajax requestgenerate thesport_choicesfor the selected event and return it as a response to the ajax request.