I have the following models defined:
class Team(models.Model):
Name = models.CharField(max_length=250)
class Fixture(models.Model):
HomeTeam = models.ForeignKey(Team, related_name="HomeTeam")
AwayTeam = models.ForeignKey(Team, related_name="AwayTeam")
HomeTeamScore = models.IntegerField(blank=True, null=True)
AwayTeamScore = models.IntegerField(blank=True, null=True)
Date = models.DateTimeField()
class Prediction(models.Model):
Fixture = models.ForeignKey(Fixture)
PredictHomeTeam = models.IntegerField()
PredictAwayTeam = models.IntegerField()
PredictionDate = models.DateTimeField()
User = models.ForeignKey(User)
So if use a filter to get 6 fixtures based on Date then I would like to pass these into a formset of PredictionForm but I am struggling on how to pass in the fixture since it is required as a foreignkey field.
This is my first django project so I guess I am also looking for confirmation on whether I am on the right lines with using formsets for this.
You want to use a model formset which works just like a normal formset but understands how to work with models.
The formset documentation describes how to use initial data with your formset:
Just as a note, use lowercase and underscores for all of your class method/attributes. Instead of this:
Do this:
This makes it easier to read and reason about your code for other programmers. You should read PEP 8 which is the official Python style guide.