【问题标题】:Python: How to create a sequence of two-tuplesPython:如何创建一个二元组序列
【发布时间】:2013-09-27 18:45:46
【问题描述】:

我正在使用 Django,但我遇到了一个我不知道如何解决的错误。我确定这是一个新手问题。我有以下数据结构,我认为它是“一个二元组序列”:

CONFERENCES = (
    ( 'AE' 'AFC East' ),
    ( 'AN' 'AFC North' ),
    ( 'AS' 'AFC South' ),
    ( 'AW' 'AFC West' ),
    ( 'NE' 'NFC East' ),
    ( 'NN' 'NFC North' ),
    ( 'NS' 'NFC South' ),
    ( 'NW' 'NFC West' ),
)

这是这样引用的:

class Conference( models.Model ): 
    conference_name = models.CharField( max_length=2, choices=CONFERENCES ) 

但是,在我运行 python manage.py validate 后,Django 给了我这个错误:

gameTrackerApp.conference: "conference_name": "choices" should be a sequence of two-tuples.

我做错了什么?

【问题讨论】:

  • 好吧,你的内部元组中没有逗号。 Python 中的每个元组至少需要一个逗号(甚至是单项元组)

标签: python django python-2.7 django-models tuples


【解决方案1】:

缺少逗号:

CONFERENCES = (
   ( 'AE', 'AFC East' ),
   ( 'AN', 'AFC North' ),
   ( 'AS', 'AFC South' ),
   ( 'AW', 'AFC West' ),
   ( 'NE', 'NFC East' ),
   ( 'NN', 'NFC North' ),
   ( 'NS', 'NFC South' ),
   ( 'NW', 'NFC West' ),
)

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多