インライン フォームセットを使用して、レシピ フォーム (単なるモデル フォーム) と RecipeIngredient フォームセットで構成されるレシピ入力フォームを作成することに成功しました。モデルは次のとおりです。
#models.py
class Recipe(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(blank=True)
directions = models.TextField()
class RecipeIngredient(models.Model):
quantity = models.DecimalField(max_digits=5, decimal_places=3)
unit_of_measure = models.CharField(max_length=10, choices=UNIT_CHOICES)
ingredient = models.CharField(max_length=100, choices=INGREDIENT_CHOICES)
recipe = models.ForeignKey(Recipe)
成分を次のように変更したい:
ingredient = models.ForeignKey(Ingredient)
成分は次のとおりです。
class Ingredient(models.Model):
title = models.CharField(max_length=100)
インライン フォームセットをセットアップするために、views.py を変更せずに残しました。
FormSet = inlineformset_factory(Recipe, RecipeIngredient, extra=1,
can_delete=False)
そして、すべてが完璧に機能しました...原料のドロップダウンをクリックすると、探していたタイトル値ではなく、原料のエントリごとに繰り返される「原料オブジェクト」の選択肢しか表示されませんでした。
この単純なアプローチを維持し、ドロップダウンに Ingredient.title を表示する方法はありますか? これにより、保存、表示などに関して他の問題が発生しますか?
それができない場合、これを機能させるために何をする必要がありますか?
皆さんありがとう。