在我的练习中,我有一个书的 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,文件 -> 模型/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