Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango loaddata ValidationError
    primarykey
    data
    text
    <p>This <a href="https://stackoverflow.com/questions/1884827/django-loaddata-error">thread</a> got too confusing (for me) so I am asking the question again. I could not make the csv2json.py script mentioned in the original question work. I am just trying to find a way to import data to sqlite3 database.</p> <p>Here's the model I am working with:</p> <pre><code>from django.db import models class School(models.Model): school = models.CharField(max_length=300) def __unicode__(self): return self.school class Lawyer(models.Model): firm_url = models.URLField('Bio', max_length=200) firm_name = models.CharField('Firm', max_length=100) first = models.CharField('First Name', max_length=50) last = models.CharField('Last Name', max_length=50) year_graduated = models.IntegerField('Year graduated') school = models.CharField(max_length=300) school = models.ForeignKey(School) class Meta: ordering = ('?',) def __unicode__(self): return self.first </code></pre> <p>(I'll fix the duplicate "school" later.)</p> <p>I have this data1.csv file:</p> <pre><code>pk,firm_url,firm_name,first,last,school,year_graduated 1,http://www.graychase.com/babbas, Gray &amp; Chase, Amr A, Babbas, The George Washington University Law School, 2005 </code></pre> <p>I have to put in "pk" and 1 manually as required by the script.</p> <p>Then, I run the script and I get the data1.csv.json file:</p> <pre><code>[ { "pk": 1, "model": "wkw2.Lawyer", "fields": { "school": "The George Washington University Law School", "last": "Babbas", "firm_url": "http://www.graychase.com/babbas", "year_graduated": "2005", "firm_name": "Gray &amp; Chase", "first": "Amr A" } } ] </code></pre> <p>I put this file in the fixtures folder in the app directory and run <code>manage.py loaddata data1.csv.json</code></p> <p>and I get the error</p> <pre><code>Installing json fixture 'data1.csv' from 'C:\~\Django\sw2\wkw2\fixtures'. Problem installing fixture 'C:\~\Django\sw2\wkw2\fixtures\data1.csv.json': Traceback (most recent call last): File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py", line 150, in handle for obj in objects: File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in Deserializer for obj in PythonDeserializer(simplejson.load(stream)): File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 95, in Deserializer data[field.attname] = field.rel.to._meta.get_fie(field.rel.field_name).to_python(field_value) File "C:\Python26\lib\site-packages\django\db\models\fields\__init__.py", line 356, in to_python_("This value must be an integer.")) ValidationError: This value must be an integer. </code></pre> <p>When I comment out the two lines in the script with pk in them I get this error message:</p> <pre><code>Installing json fixture 'data1.csv' from 'C:\~\Django\sw2\wkw2\fixtures'. Problem installing fixture 'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\data1.csv.json': Traceback (most recent call last): File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py", line 150, in handle for obj in objects: File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in Deserializer for obj in PythonDeserializer(simplejson.load(stream)): File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 77, in Deserializer data = {Model._meta.pk.attname : Model._meta.pk.to_pytho(["pk"])} KeyError: 'pk' </code></pre> <p>What am I doing wrong?</p> <p><b>Edit:</b></p> <p>I took out the quotes around the year to make it an integer but I still got the same ValidationError:</p> <pre><code>... "year_graduated": 2005, ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload