django後台批量導入資料
在生產環境中,往往資料不是幾條或幾百條,那麼舉個例子,將公司所有員工員工號碼或帳號密碼導入進後台,那就不建議你去後台,那就不建議你去後台一筆記錄去新增了
如何從xml中批次匯入svn記錄
第一步:
為資料建立模型
reee為資料建立模型
reee
為資料建立模型了建立接受我們xml檔的models@python_2_unicode_compatible class SVNLog(models.Model): vision = models.IntegerField(verbose_name=u"修订版本", blank=False, null=False,) author = models.CharField(verbose_name=u"作者", max_length=60, blank=True, null=True) date = models.DateTimeField(verbose_name=u"修订时间",null=True ) msg = models.TextField(verbose_name=u"注释消息", blank=False, null=False, default=u"") paths = models.TextField(verbose_name=u"影响的文件", blank=False, null=False, default=u"") created_time = models.DateTimeField(verbose_name=u"创建时间", auto_now_add=True, ) update_time = models.DateTimeField(verbose_name=u"修改时间", auto_now=True, ) class Meta: ordering = ['revision'] def __str__(self): return u'r%s' % (self.revision or u"", )
ok,以上程式碼我們定義好了資料和上傳檔案的model
@python_2_unicode_compatible class ImportLogFile(models.Model): LogFile = models.FileField(upload_to='LogFile') FileName = models.CharField(max_length=50, verbose_name=u'文件名') class Meta: ordering = ['FileName'] def __str__(self): return self.FileName
因為我們上傳文件,讀取文件,解析文件,操作資料庫合為一步來操作,大家可以打開debug,在上傳文件的時候,返回參數的obj裡包括了文件上傳的路徑,這個路徑也是下一步我們操作解析文件的關鍵,好了我們在這個app資料夾下新建一個utils.py 用來操作我們操作檔案和資料庫的工具類,為了簡單我寫成了函數如下
先貼一下我們要測試的xml檔案python manage.py makemigrations python manage.py migrate
class ImportLogAdmin(admin.ModelAdmin): list_display = ('LogFile','FileName',) list_filter = ['FileName',] def save_model(self, request, obj, form, change): re = super(YDImportLogAdmin,self).save_model(request, obj, form, change) update_svn_log(self, request, obj, change) return re
輸出結果格式
qwert2016-09-27T07:16:37.396449Z/aaa/README20160927 151630VisualSVN Server2016-09-20T05:03:12.861315Z/branches/tags/trunkhello word
r2 | qwer | 2016-09-27 15:16:37 +0800 (二, 27 9 2016) | 1 line Changed paths: A /xxx/README 20160927 151630 ------------------------------------------------------------------------ r1 | VisualSVN Server | 2016-09-20 13:03:12 +0800 (二, 20 9 2016) | 1 line Changed paths: A /branches A /tags A /trunk Initial structure. from .models import SVNLog import xmltodict def update_svn_log(self, request, obj, change): headers = ['r','a','d','m','p'] filepath = obj.LogFile.path xmlfile = xmltodict.parse(open(filepath, 'r')) xml_logentry = xml.get('log').get('logentry') info_list = [] pathlist = [] sql_insert_list = [] sql_update_list = [] for j in xml: data_dict = {} # get path paths = j.get('paths').get('path') if isinstance(paths,list): for path in paths: action = path.get('@action') pathtext = path.get('#text') pathtext = action + ' ' + pathtext pathlist.append(pathtext) _filelist = u'\n'.join(pathlist) _paths = u"Changed paths:\n {}".format(_filelist) print _paths else: _filelist = paths.get('@action') + ' ' + paths.get('#text') _paths = u"Changed paths:\n {}".format(_filelist) print _paths # get revision vision = j.get('@vision') # get auth author = j.get('author') #get date date = j.get('date') #get msg msg = j.get('msg') data_dict[headers[0]] = int(vision) data_dict[headers[1]] = author data_dict[headers[2]] = date data_dict[headers[3]] = msg data_dict[headers[4]] = _paths info_list.append(data_dict) _svnlog = SVNLog.objects.filter().order_by('-vision').first() _last_version = _svnlog.vision if _svnlog else 0 for value in info_list: vision = value['r'] author = value['a'] date = value['d'] msg = value['m'] paths = value['p'] print vision,author _svnlog = YDSVNLog.objects.filter().order_by('-revision').first() _last_version = _svnlog.revision if _svnlog else 0 if vision > _last_version: sql_insert_list.append(SVNLog(revision=revision, author=author, date=date, msg = msg, paths = paths)) else: sql_update_list.append(SVNLog(revision=revision, author=author, date=date, msg = msg, paths = paths)) SVNLog.objects.bulk_create(sql_insert_list) SVNLog.objects.bulk_create(sql_update_list)
我們使用的xmltodict這個第三方函式庫來解析xml,他把內容解析成了高效率的orderdict類型,就是有序列的字典
這個xml中比較複雜的是那個paths裡的path,因為這個xml包含兩個元素,第一個元素的path只含有一個path,第二個元素中的paths包含有三個path,所以我們在解析取得的時候需要判斷paths = j.get('paths').get('path') if isinstance(paths,list): pass
revision = j.get('@vision') # get auth author = j.get('author') #get date date = j.get('date') #get msg msg = j.get('msg')