-1
我編寫了一個函數來將xml文件中的數據保存到數據庫中。我使用了兩種模型,請參閱下面的內容。xml使用2個表進行解析
models.py
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age = models.IntegerField()
class Book(models.Model):
book_id=models.AutoField(primary_key=True,unique=True)
book_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
我的.xml文件是
<book>
<book_id>101</book_id>
<book_name>Python</book_name>
<publisher_name>Maxwell</publisher_name>
<author_id>1002</author_id>
<first_name>John</first_name>
<last_name>Dezosa</last_name>
<email>[email protected]</email>
<age>34</age>
</book>
<book>
<book_id>102</book_id>
<book_name>Django</book_name>
<publisher_name>Technical</publisher_name>
<author_id>1003</author_id>
<first_name>Josep</first_name>
<last_name>Raj</last_name>
<email>[email protected]</email>
<age>29</age>
</book>
我需要什麼樣的方法是將XML數據保存到相應的fields.From谷歌我學到了使用單個表配對,在這裏我不知道如何爲「作者」表創建對象。
我試着用下面的代碼
for books in xmlDocTree.iter('book'):
book_id = books[0].text
book_name = books[1].text
publisher_name = books[2].text
author_id = books[3].text
books.first_name = books[0].text
books.last_name = books[1].text
books.email = books[2].text
books.age = books[3].text
得到以下回溯
AttributeError at /addxml/
first_name
Request Method: POST
Request URL: http://localhost:8000/addxml/
Django Version: 1.3.7
Exception Type: AttributeError
Exception Value:
first_name
Exception Location: /root/Samples/DemoApp/DemoApp/views.py in addxml, line 106
感謝
另一列我校如你所說,看到我正在此錯誤ParseError在/ addxml/ 垃圾文檔元素之後:13行,列2 – user2086641 2013-04-05 14:42:45
在這一行中得到上述錯誤xmlDocTree = etree.XML(xmlDocData) – user2086641 2013-04-05 14:57:00
你得到的錯誤是什麼? – karthikr 2013-04-05 15:25:11