【发布时间】:2017-10-29 09:46:30
【问题描述】:
我的models.py 中有以下代码:
class Application(models.Model):
team_name = models.CharField(max_length=100)
...
class Participant(models.Model):
name = models.CharField(max_length=100)
grade = models.SmallIntegerField(choices=GRADE_CHOICES, blank=True, null=True)
application = models.ForeignKey(Application, related_name="participants")
...
即有多个Participants,每个Participant 将位于一个应用程序上。 (定义为每个Application 正好有8 个Participants)。
此代码的预期目的是用户将能够创建一个新应用程序,他/她将在该应用程序上命名为 8 Participants。在创建Application 时,还应创建并链接8 个Participants 的新实例,以便每个Participant 都有新创建的Application 作为其外键。
我目前正在努力创建一个可以处理此创建过程的表单/视图。我希望向用户呈现一个单独的网页,该网页呈现输入表单,允许用户创建一个新的Application(名称等),以及 8 个新的Participants(每个都有名称、等级等)。 )
我当前的forms.py 有:
class ApplicationForm(forms.ModelForm):
"""
A form that edits an application and all of its participants.
"""
nationality = forms.CharField(disabled=True, required=False)
class Meta:
model = Application
fields = '__all__'
但我意识到这不会向用户显示创建此Application 需要创建8 个新Participants。如何修改我的forms.py 以包含空间供用户通过创建Application 创建8 个Participants?
我能想出的唯一解决办法是在一个视图中手动列出8个Participant表单调用,但我觉得一定有更好的解决方案。
【问题讨论】:
标签: python django forms django-forms django-views