WebFaction
Community site: login faq

Hi,I am new to xmp paring,i am trying to save the data from xml file to mysql database.

I learned about deserialization in django

my book.xml is

 <?xml version="1.0"?>
    <bookstore>
      <book>
        <book_id>101</book_id>
        <book_name>Python</book_name>
        <author_name>Geroge</author_name>
        <publisher_name>Maxwell</publisher_name>
        <email>george@gmail.com</email>
        <bookref>efghi53261</bookref>
      </book>
      <book>
        <book_id>102</book_id>
        <book_name>Django</book_name>
        <author_name>John</author_name>
        <publisher_name>Technical</publisher_name>
        <email>john@gmail.com</email>
        <bookref>eyghq06283</bookref>
      </book>  
    </bookstore>

I created a models in my app with the above fields.So i want to save the above xml data to the appropriate field.

For that my views.py is

 def xmldata(request):
    path = "{0}/fixtures/book.xml".format(settings.PROJECT_ROOT)
    xmlDoc = open(path, 'r')
    xmlDocData = xmlDoc.read()
    xmlDocTree = etree.XML(xmlDocData)

    for books in xmlDocTree.iter('book'):
        book_id = books[0].text
        book_name = books[1].text 
        author_name = books[2].text
        publisher_name = books[3].text
        email = books[4].text
        bookref = books[5].text   Book.objects.create(book_id=book_id,book_name=book_name,author_name=author_name,publisher_name=publisher_name,email=email,bookref=bookref)

models.py is

class Book(models.Model):

    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    author_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    email = models.EmailField()
    bookref = models.CharField(max_length=10)
    class Meta:
        db_table = u'Book'

With the guidence from you i had modefied the code sir.

The aboce code is not working,i am not getting amy error but not saving the data into database.Can you see what would be the problem

Thanks

asked 04 Apr '13, 10:02

pychu
3158
accept rate: 0%

edited 04 Apr '13, 23:22

can i get an answer for this,please

(04 Apr '13, 10:34) pychu

Your not saving a queryset, your saving the XML object, which makes no sense.

Querysets are how Django interacts with a model, and models are how Django interacts with the database by defining the schema.

Create a model which contains fields for all of the data you want to save, than put a queryset which saves to that model in your view.

permanent link

answered 04 Apr '13, 19:16

johns
5.4k412
accept rate: 23%

edited 04 Apr '13, 19:17

Johns,i edited the above question,please help me to d o this task.

(04 Apr '13, 23:24) pychu

It is hard to do it without having it live to debug it, but It looks like you are now missing the .save() on the queryset.

add a .save() to the last line behind the query set. Something to the effect of,

whatever = Book.objects.create(book_id=book_id,book_name=book_name,author_name=author_name,publisher_name=publisher_name,email=email,bookref=bookref)
whatever.save()

You will want to check the official Django documentation for more info and examples.

(05 Apr '13, 01:04) johns
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×910
×337
×11
×4

question asked: 04 Apr '13, 10:02

question was seen: 5,450 times

last updated: 05 Apr '13, 01:04

                              
WEBFACTION
REACH US
SUPPORT
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM