假设我有一个模型:
class Team(models.Model):
name = models.CharField(max_length=20)
class Game(models.Model):
title = models.CharField(ma_length=20)
home_team = models.ForeignKey(Team)
away_team = models.ForeignKey(Team)
然后在我看来,我有:
def manage_games(request):
GameFormSet = modelformset_factory(Game, extra=1)
game_forms = GameFormSet(request.POST or None,
queryset=Game.objects.all())
if request.method == "POST":
if game_forms.is_valid():
game_forms.save()
game_forms = GameFormSet(queryset=Game.objects.all())
return render(request, "admin_dashboard/manage_games.html", locals())
这可以正常工作,除了我希望能够将home_team
andaway_team
变成 CharFields(而不是现在的下拉菜单),同时还能将它们保持为单独的模型。如何将内联模型表单集添加到模型表单集中以使其成为可能?