English 中文(简体)
采用多种模式的模拟格式
原标题:Pre-Populating Model-Formsets With Multiple Model Instances

我在学习大坝的同时,正在建造一个足球预测器,其模式如下:

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)

我有许多固定装置物体有人居住,并且一直在使用基于预测模型的模型表格,以便形成一种使用户能够进入计分的观点。

问题是,他们必须选择与预测有关的哪一种办法。 我愿预先欢迎这一点,以便他们获得固定装置清单,并刚刚进入屋顶和离层。 这需要预先策划。 Fixture field and Prediction. 运动员们,但我对如何做到这一点感到不满?

任何帮助都受到高度赞赏。

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.

问题回答

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

你的法典将考虑这样的问题:

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 their field name are not PEP 8 compliance. 他们应该受到强调,但你也发出呼吁。 ( http://www.python.org/dev/peps/pep-0008/)





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签