Question

I am having a little trouble with importing initial data through xml files. For example I name this file in myapp/fixtures/initial_data.xml:

<?xml version="1.0" encoding="UTF-8"?>
<rows>
  <row>
    <model>myapp.nutrition</model>
    <name>Asiago Cheese Bagel</name>
    <calories>370</calories>
    <protein >17</protein >
    <carbs>56</carbs>
    <fats>8</fats>
    <restaurant >Au Bon Pain</restaurant >
    <price>1.29</price>
  </row>
</rows>

And this is what my model file looks like:

from django.db import models
class Nutrition(models.Model):
     name= models.CharField(max_length=100)
     calories= models.IntegerField()
     protein= models.IntegerField()
     carbs= models.IntegerField()
     fats= models.IntegerField()
     restaurant= models.CharField(max_length=100)
     price= models.DecimalField(decimal_places=2, max_digits=10)

When I run manage.py loaddata myapp/fixtures/initial_data.xml, I get: Installed 0 object(s) from 0 fixture(s). I have also tried JSON and got the same result. Any ideas?

Was it helpful?

Solution

You should format the XML as Django expects it. One way to go is to manually add and object using Django's admin and then dump the data using

manage.py dumpdata --format=xml --indent=4

For your case, this gives something like

<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
    <object pk="1" model="myapp.nutrition">
        <field type="CharField" name="name">Asiago Cheese Bagel</field>
        <field type="IntegerField" name="Calories">370</field>
        ...
    </object>
</django-objects>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top