如何向现有模型添加新数据?

时间:2017-09-07 09:19:39

标签: python django excel

我想解析excel&制作字典并连接具有相同user_id字典的模型(用户)。我解析了excel并得到了列表(copy()),如rows2我想将此列表数据添加到用户模型。 ['1', 'Bob', 'America', '', '', 0.0]

views.py

在最后一段代码之后我应该写什么? models.py是

我在models.py。

中创建了区域数据模型
#coding:utf-8
from django.shortcuts import render
import xlrd
from .models import User

book = xlrd.open_workbook('../data/excel1.xlsx')
sheet = book.sheet_by_index(1)

def build_employee(employee):
  if employee == 'leader':
     return 'l'
  if employee == 'manager':
     return 'm'
  if employee == 'others':
     return 'o'

for row_index in range(sheet.nrows):
  rows = sheet.row_values(row_index) 
  is_man = rows[4] != ""
  emp = build_employee(rows[5])
  user = User(user_id=rows[1], name_id=rows[2], name=rows[3], 
              age=rows[4],man=is_man,employee=emp)
  user.save()

book2 = xlrd.open_workbook('../data/excel2.xlsx')
sheet2 = book2.sheet_by_index(0)
headers = sheet2.row_values(0)

large_item = None
data_dict = {}
for row_index in range(sheet2.nrows):
    rows2 = sheet2.row_values(row_index)
    large_item = rows2[1] or large_item

我想把#coding:utf-8 from django.db import models class User(models.Model): name = models.CharField(max_length=200,null=True) age = models.CharField(max_length=200,null=True) area = models.ForeignKey(Area) country = models.CharField(max_length=50, choices=TYPE_CHOICES) a = models.CharField(max_length=10, choices=TYPE_CHOICES) b = models.CharField(max_length=10, choices=TYPE_CHOICES) c = models.CharField(max_length=10, choices=TYPE_CHOICES) 放在国家/地区,rows2[2]放在a,rows2[3]放入b,rows2[4]放入c。

0 个答案:

没有答案