Note that there are some explanatory texts on larger screens.

plurals
  1. POPre-Populating Model-Formsets With Multiple Model Instances
    text
    copied!<p>I am building a football predictions app whilst learning django and have the following models:</p> <pre><code>class Team(models.Model): Name = models.CharField(max_length=30) class Fixture(models.Model): HomeTeam = models.ForeignKey(Team, related_name='HomeTeamRef') AwayTeam = models.ForeignKey(Team, related_name='AwayTeamRef') HomeTeamScore = models.IntegerField(null=True, blank=True) AwayTeamScore = models.IntegerField(null=True, blank=True) Date = models.DateField() class Player(models.Model): User = models.ForeignKey(User) DefaultHomeScore = models.IntegerField() DefaultAwayScore = models.IntegerField() class Prediction(models.Model): Fixture = models.ForeignKey(Fixture) HomeTeamScore = models.IntegerField() AwayTeamScore = models.IntegerField() Date = models.DateTimeField() Player = models.ForeignKey(Player) </code></pre> <p>I have many fixture objects populated and have been using model formsets based on the Prediction model to render a view which allows the user to enter scores. </p> <p>The problem is that they must choose which fixture the prediction relates to. I would like to pre-populate this so they get a list of fixtures and just enter the hometeamscore and awayteamscore. This involves pre-poulating the Prediction.Fixture field and Prediction.Player field but I am unsure on how to go about this?</p> <p>Any help is much appreciated.</p> <p>Edit: The problems seems to be how to pass multiple instances of Fixture into a Prediction model formset, I have seen examples of passing one but would like to do this all in one go. I also would like the user to be able to make one Prediction per Fixture.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload