使用多个模型实例预填充模型表单集

发布于 2024-12-11 19:28:02 字数 1157 浏览 0 评论 0原文

我正在学习 django 的同时构建一个足球预测应用程序,并具有以下模型:

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)

我填充了许多固定对象,并且一直在使用基于预测模型的模型表单集来渲染允许用户输入分数的视图。

问题是他们必须选择预测与哪个装置相关。我想预先填充此内容,以便他们获得赛程列表,然后输入主队得分和客队得分。这涉及预先填充 Prediction.Fixture 字段和 Prediction.Player 字段,但我不确定如何进行此操作?

非常感谢任何帮助。

编辑:问题似乎是如何将 Fixture 的多个实例传递到预测模型表单集中,我已经看到传递一个的示例,但想一次性完成这一切。 我还希望用户能够对每个灯具进行一次预测。

I am building a football predictions app whilst learning django and have the following models:

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)

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.

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?

Any help is much appreciated.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-12-18 19:28:02

我想这就是您正在寻找的:
https://docs. djangoproject.com/en/1.7/topics/forms/formsets/#using-initial-data-with-a-formset

你的代码看起来像这样:

initial_data = []
for fixture in fixtures:
    initial_data.append({'Player':player,'Fixture':fixture})

formset = MyPredictionFormset(initial=initial_data)

PS 不要迂腐,但你的字段名称不符合 PEP 8。它们应该是小写并带有下划线,但这由您决定。 (http://www.python.org/dev/peps/pep-0008/< /a>)

I think this is what you are looking for:
https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#using-initial-data-with-a-formset

Your code would look something like this:

initial_data = []
for fixture in fixtures:
    initial_data.append({'Player':player,'Fixture':fixture})

formset = MyPredictionFormset(initial=initial_data)

P.S. Not to be pedantic, but your field names are not PEP 8 compliant. They should be lowercase with underscores, but it's your call. (http://www.python.org/dev/peps/pep-0008/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文