WebFaction
Community site: login faq

I written a function to save the data in xml file to database.I am using two models ,see the below

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)

I don't know how to create the value for 2nd table and save it to the database.

views.py for xml parsing

def addxml(request):
    if request.POST:
        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
            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

            book=Book.objects.create(book_id=book_id,book_name=book_name,
                publisher_name=publisher_name,author_id=author_id,first_name=first_name,last_name=last_name,email=email,age=age)
            book.save()
            books = Book.objects.all()
    return redirect('/index/')

while running the app i am getting the following traceback

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

Can anyone please check this to make it work.My problem is not able to create the object for 2nd table ie,author table and save the dta to database.

Please give me a code to how to do

Thanks

asked 05 Apr '13, 09:07

pychu
3148
accept rate: 0%


It looks to me like you're passing fields for your Author model (i.e., first_name, last_name, email, and age) to Book.objects.create. Since those fields don't exists for Book, you get the AttributeError.

Instead, you should get the Author separately, by first checking if the author exists and getting it if it does, or by creating it with Author.objects.create if it doesn't already exist.

After you have the author, pass it to Book.objects.create as the author argument.

permanent link

answered 05 Apr '13, 10:45

seanf
12.2k41836
accept rate: 37%

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:

×909
×337
×30
×11
×4

question asked: 05 Apr '13, 09:07

question was seen: 1,868 times

last updated: 05 Apr '13, 10:45

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 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