Reputation: 23
I'm trying to create a form to input scores for a round of golf. A standard round of golf is 18 holes, so the form should create 18 instances of 'Score' once submitted. How would I go about creating a form that contains a single dropdown for 'player' and 18 text fields for strokes on each hole? Below are the models being used:
class Score(models.Model):
hole = models.ForeignKey(Hole, on_delete=models.CASCADE)
player = models.ForeignKey(Player, on_delete=models.CASCADE)
strokes = models.IntegerField(default=0)
class Player(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Hole(models.Model):
number = models.IntegerField()
Upvotes: 0
Views: 71
Reputation: 551
Have a look at Django Inline Formsets. This allows you to include multiple forms per view and the inline formset allows you to work with objects related by a foreign key.
It is relatively easier when you know the exact amount of forms you need to include in your template. However, if you want to dynamically include forms in your template then you will need to look into adding some javascript to provide this functionality.
Upvotes: 2