私の演習では、フィールド「ジャンル」を持つ本のDjangoモデルがあります。このフィールドには、次のオプションの選択肢があります
GENRES_CHOICE = (
('ADV','Adventure'),
('FAN','Fantasy'),
('POE','Poetry'),
)
モデルフィールドは
genre = models.CharField(max_length = 3, blank = False, choices = GENRES_CHOICE, db_index = True, editable = False)
私のテンプレートでは、ジャンル (アドベンチャー、ファンタジー、詩) のリストをユーザーに表示し、利用可能なキーをパラメーターとして使用できるようにしたいと考えています。
そのためには、データ構造 GENRES_CHOICE を返す関数が必要ですが、できません。この問題を解決するには?
編集:コードの詳細
appname= mybookshelf、ファイル -> models/Book.py
# possible choices for the gerne field
GENRES_CHOICE = (
('ADV','Adventure'),
('FAN','Fantasy'),
('POE','Poetry'),
)
class Book(models.Model):
"""
This is the book model
...
## ATTRIBUTES (better use init, but in Django not always possible)
id = models.CharField(max_length = 64, blank = False, unique = True, primary_key = True, editable = False)
""" unique id for the element """
genre = models.CharField(max_length = 3, blank = False, choices = GENRES_CHOICE, db_index = True, editable = False)
""" book genre """
published_date = models.DateField(null = True, auto_now_add = True, editable = False)
""" date of publishing """
次に、別のファイルに、私が持っている MyFunctions.py としましょう
from mybookshelf.models import GENRES_CHOICE
def getBookCategories():
"""
This function returns the possible book categories
categories = GENRES_CHOICE
return categories