CSV导入会跳过重复的记录

时间:2013-03-15 18:06:32

标签: django csv

我正在使用django-adaptors我需要一种方法来检查重复的手机号码,并跳过导入这些记录,同时继续将其余内容添加到CSV文件中。

这是我目前的CsvModel.py

class ContactCSVModel(CsvModel):

    first_name = CharField()
    last_name = CharField()
    company = CharField()
    mobile = CharField()
    # groups = DjangoModelField(Groups)

    class Meta:
        delimiter = "^"
        dbModel = Contacts

这是导入

  # Try to import CSV
            ContactCSVModel.import_from_file(self.filepath)

2 个答案:

答案 0 :(得分:3)

您正在寻找的是元选项中的更新采用此示例...

class Meta:
        delimiter = ','
        dbModel = Product
        update = {
            'keys': ['name']   # or mfr_code, main_photo_url, etc..., price
        }

会做的伎俩。

答案 1 :(得分:1)

我不知道如何使用第三方应用程序这么简单。只需编写一个管理命令(将其命名为myapp / management / commands / csvimport.py):

from django.core.management.base import BaseCommand, CommandError
from fabric.colors import _wrap_with
from optparse import make_option
import os, csv


green_bg = _wrap_with('42')
red_bg = _wrap_with('41')

class Command(BaseCommand):
    help = "Command to import a list of stuff"
    option_list = BaseCommand.option_list + (
        make_option(
            "-f", 
            "--file", 
            dest = "filename",
            help = "specify import file", 
            metavar = "FILE"
        ),
    )

    def handle(self, *args, **options):
        # make sure file option is present
        if options['filename'] == None :
            raise CommandError("Option `--file=...` must be specified.")

        # make sure file path resolves
        if not os.path.isfile(options['filename']) :
            raise CommandError("File does not exist at the specified path.")

        # print file
        print green_bg("Path: `%s`" % options['filename'])

        # open the file
        with open(options['filename']) as csv_file:
            reader = csv.reader(csv_file)
            for row in reader:

                try :
                    object, created = Contacts.objects.get_or_create(
                        mobile = row[3],
                        defaults={
                            "first_name": row[0],
                            "last_name": row[1],
                           "company": row[2],
                        }
                    )
                except:
                    print red_bg("Contacts `%s` could not be created." % row['mobile'])

运行这个也很容易:

python manage.py csvimport --file=path/to/myfile.csv

其中csvimport是管理命令的文件名。