2015-10-10 55 views
0

我想將csv文件導入到我的SQL數據庫。這裏是我的模型:用Django適配器導入CSV文件:Db模型不應該定義任何csv字段

class Matchups(models.Model): 
    name = models.CharField(max_length=140) 
    vs_Ashe = models.CharField(max_length=140) 

    def __unicode__(self): 
     return "%s" % (self.name) 

class MyCsvModel(CsvDbModel): 
    name = CharField() 
    vs_Ashe = CharField() 

    class Meta: 
     dbModel = Matchups 
     delimiter = "," 

試圖導入CSV文件:my_csv_list = MyCsvModel.import_data(data = open("/home/irmantas/ADC.csv"))

我得到這個錯誤:

A Db model should not have any csv field defined. 

問題是什麼?

回答

0

您使用 'CsvDbModel',不應再定義字段,你的類應該是:

class MyCsvModel(CsvDbModel): 

    class Meta: 
    dbModel = Matchups 
    delimiter = "," 

否則,你應該使用 'CsvModel',而不是:

class MyCsvModel(CsvModel): 
    name = CharField() 
    vs_Ashe = CharField() 

    class Meta: 
    dbModel = Matchups 
    delimiter = ","