如何手动关联两个模型?从 excel 电子表格中提取的数据
How to manually associate two models? Data pulled from excel spreadsheet
我正在尝试创建一个网络表单,用户可以在其中输入几条信息并上传一个 excel 电子表格。在 post 期间(在我的 views.py 中)我试图打开文件并将该文件中的信息保存到我的模型中。
另一件事是两个模型关联在一起。任何人都知道 views.py 的外观或这是否可能?
models.py
class service_order(models.Model):
r_name = models.CharField("Requestor Name", max_length=25)
r_email = models.CharField("Requestor Email Contact", max_length=25)
class specifications(models.Model):
service_order = models.ForeignKey(service_order)
controller_type = models.CharField("Controller", max_length=25)
hostname = models.CharField("Hostname", max_length=25)
excel 文件很简单:
"controller", "hostname", "domain", "ip_address"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
我正计划将 xlrd 合并到我的 django 表单和视图中。这是我用来阅读 excel 电子表格的一些初步代码:
import xlrd
datafile = "sample_survey.xlsx"
def parse_file(datafile):
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
START_ROW = 35
END_ROW = 60
myList = []
for row in range(START_ROW,END_ROW):
values = (sheet.row_values(row, start_colx=1, end_colx=20))
headers = ["controller", "hostname", "domain", "ip_address", "netmask", "gateway", "dns1", "dns2", "ntp1", "ntp2",
"order_name", "order_phone", "order_email",
"shipping_adress", "shipping_city", "shipping_region", "shipping_region_code", "shipping_country", "shipping_diff"]
dictionary = dict(zip(headers, values))
myList.append(dictionary)
for row in myList:
print row
data = parse_file(datafile)
如果每一行代表不同的顺序,那么代码将很简单:
for row in myList:
order = service_order.objects.create(r_name=row['order_name'],
r_email=row['order_email'])
specifications.objects.create(service_order=order,
controller_type=row['controller'],
hostname=row['hostname'])
另请注意,在 parse_file()
中您必须接受文件内容而不是文件名:
def parse_file(file_contents):
workbook = xlrd.open_workbook(file_contents=file_contents)
...
def my_view(request):
data_file = request.FILES['data_file']
parse_file(data_file.read())
...
我正在尝试创建一个网络表单,用户可以在其中输入几条信息并上传一个 excel 电子表格。在 post 期间(在我的 views.py 中)我试图打开文件并将该文件中的信息保存到我的模型中。
另一件事是两个模型关联在一起。任何人都知道 views.py 的外观或这是否可能?
models.py
class service_order(models.Model):
r_name = models.CharField("Requestor Name", max_length=25)
r_email = models.CharField("Requestor Email Contact", max_length=25)
class specifications(models.Model):
service_order = models.ForeignKey(service_order)
controller_type = models.CharField("Controller", max_length=25)
hostname = models.CharField("Hostname", max_length=25)
excel 文件很简单:
"controller", "hostname", "domain", "ip_address"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
"hp server", "sample hostname", "google.com "1.2.3.4"... etc
我正计划将 xlrd 合并到我的 django 表单和视图中。这是我用来阅读 excel 电子表格的一些初步代码:
import xlrd
datafile = "sample_survey.xlsx"
def parse_file(datafile):
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
START_ROW = 35
END_ROW = 60
myList = []
for row in range(START_ROW,END_ROW):
values = (sheet.row_values(row, start_colx=1, end_colx=20))
headers = ["controller", "hostname", "domain", "ip_address", "netmask", "gateway", "dns1", "dns2", "ntp1", "ntp2",
"order_name", "order_phone", "order_email",
"shipping_adress", "shipping_city", "shipping_region", "shipping_region_code", "shipping_country", "shipping_diff"]
dictionary = dict(zip(headers, values))
myList.append(dictionary)
for row in myList:
print row
data = parse_file(datafile)
如果每一行代表不同的顺序,那么代码将很简单:
for row in myList:
order = service_order.objects.create(r_name=row['order_name'],
r_email=row['order_email'])
specifications.objects.create(service_order=order,
controller_type=row['controller'],
hostname=row['hostname'])
另请注意,在 parse_file()
中您必须接受文件内容而不是文件名:
def parse_file(file_contents):
workbook = xlrd.open_workbook(file_contents=file_contents)
...
def my_view(request):
data_file = request.FILES['data_file']
parse_file(data_file.read())
...