如何解决Django模型中的“选择必须是可迭代的”错误?

时间:2019-04-08 05:16:15

标签: python django-models

我正在尝试将我的应用迁移到数据库,但该错误不允许我 我应该在哪里进行更改?

我已经查看了其他决定。没什么可以帮助我

class Movie(models.Model):                              
        NON_RATED = 0                                   
        RATED_G = 1                                     
        RATED_PG = 2                                    
        RATED_R = 3                                     
        RATINGS = (                                    
            (0, 'NR-not_rated'),                        
            (1, 'G-General_Audiences'),                 
            (2, 'PG-Parental_Guidances', 'Suggested'),  
            (3, 'R-Restricted')                         
        )          
        rating = models.IntegerField(     
                choices=RATINGS,            
                default=0)   

1 个答案:

答案 0 :(得分:0)

选择属性采用listtuple of 2 pairs。您不能拥有第三值。

class Movie(models.Model):                              
        NON_RATED = 0                                   
        RATED_G = 1                                     
        RATED_PG = 2                                    
        RATED_R = 3                                     
        RATINGS = (                                    
            (0, 'NR-not_rated'),                        
            (1, 'G-General_Audiences'),                 
            (2, 'PG-Parental_Guidances', 'Suggested'),  # you should remove Suggested here. 
            (3, 'R-Restricted')                         
        )          
        rating = models.IntegerField(     
                choices=RATINGS,            
                default=0) 

如果您想要另一个,可以尝试(2, ('PG-Parental_Guidances', 'Suggested')),但是由于内部结构,这也会在某些默认值中产生错误。