使用django-adapters将csv文件导入django模型

时间:2014-07-08 22:56:36

标签: python django csv

models.py

from django.db import models

class Module(models.Model):
    modulename = models.CharField(max_length=10)
    moduledesc = models.CharField(max_length=10)
    metamodule = models.CharField(max_length=10)
    version = models.IntegerField()
    recordcreated = models.DateField(auto_now=True)
    record_created_by = models.CharField(max_length=10)
    class Meta:`enter code here`
        verbose_name = "Module"
    def __str__(self):
        return self.modulename

我的csv课程

from adaptor.model import CsvModel
from adaptor.fields import CharField, IntegerField, DecimalField, DateField

from my_app1.models import Module

class ModuleCsvModel(CsvModel):

    modulename = CharField()
    moduledesc = CharField()
    metamodule = CharField()
    version = IntegerField()
    recordcreated = DateField(**{'format':'%Y-%m-%d'})
    recordcreatedby = CharField()

    class Meta:
            dbModel = Module
            delimiter = ","
            has_header = True

我尝试了以下导入命令来导入csv filecdata。但我收到错误如下

>>> from my_app1.adaptor import ModuleCsvModel
>>> ModuleCsvModel.import_data(data = open("module.csv"))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'import_data'

请帮助我在哪里受到打击,如果需要更多信息,请告诉我。

0 个答案:

没有答案