【问题标题】:how to save data from xml file to postgresql database in django 1.9?如何在 django 1.9 中将数据从 xml 文件保存到 postgresql 数据库?
【发布时间】:2017-01-23 08:37:40
【问题描述】:

我有一个名为 order-service-api-inbound-sample.xml

的 xml 文件

谁的文件路径是 /home/bs-086/Django/mh-portal/master/portal/portal/endpoints/testdata/order-service-api-inbound-sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="file:/C:/Users/COMP/Desktop/purchase_order_inbound_schema.xsd">
    <customer>
        <customer-name>jubydull</customer-name>
        <customer-address1>Bangladesh</customer-address1>
        <customer-address2>dhaka</customer-address2>
        <customer-city>dhaka</customer-city>
        <customer-state></customer-state>
        <customer-zip>1205</customer-zip>
        <customer-country>Bangladesh</customer-country>
    </customer>
</purchaseOrder>

我有一个名为 models.py 的模型类,其文件路径为 /home/bs-086/Django/mh-portal/master/portal/portal/endpoints/models.py

from __future__ import unicode_literals
from django.db import models

class Customer(models.Model):
    name = models.CharField(max_length=120)
    address1 = models.CharField(max_length=120)
    address2 = models.CharField(max_length=120)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    zip = models.IntegerField()
    country = models.CharField(max_length=50)

    def __str__(self):
        return self.name

现在我想将 xml 文件值保存到我的模型中。我该怎么做?我正在使用 postgresql 数据库。

【问题讨论】:

  • 很多不同的可能性,编写自定义反序列化器,使用 xslt 转换为标准 django 转储数据格式。将 xml 加载到 dom 中并从中创建对象。使用它的 xml 函数直接加载到 postgresql 中,....
  • 给我任何资源。我是新的 Django @e4c5

标签: xml postgresql python-2.7 django-1.9


【解决方案1】:

这取决于你!

  1. 如果您想从视图中执行此操作,您可以在视图中创建一个函数并在呈现视图时运行它。
  2. 以表单形式导入文件并解析 XML 文件,然后将数据存储到数据库表中。与数字 1 相同,但该函数应在 form.py 中运行
  3. 如果您想自动执行此操作,例如从 XML 文件填充您的数据库,无需任何视图或表单。在这种情况下,您可以将函数放在 models.py 中,在创建所有模型后运行该函数!!我知道,这种方式有点奇怪!!

您可以使用xml.etree.ElementTree 库,默认情况下它位于 python 库中。 不要忘记将它添加到setting.py中的INSTALLED_APPS列表

这是你可以使用的功能:

import xml.etree.ElementTree as ET

def saveXML2db():
   file_dir = '<PATH_TO_YOUR_XML_FILE>'
   purchaseOrder =  ET.parse(file_dir)
   customers = purchaseOrder.findall("customer")
   for customer in customers:
       name = customer.find("customer -name").text
       address1 = customer.find("customer-address1").text
       address2 = customer.find("customer-address2").text
       city = customer.find("customer-city").text
       state = customer.find("customer-state").text
       zip2 = customer.find("customer-zip").text
       zip = int(zip2)
       country = customer.find("customer-country").text
       x = Customer.objects.create(name=name, address1=address1, address2=address2, city=city, state=state, zip=zip, country=country)
       x.save()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2011-02-19
    • 2020-09-15
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多