So I'm trying to run a script from the command line that parses information from an xml file stored locally. Whenever I run the script I get this error.
Traceback (most recent call last):
File "updatedb.py", line 54, in <module>
populate()
File "updatedb.py", line 37, in populate
diameter=diameter, streetNumber=streetNumber, street=street)
File "updatedb.py", line 41, in add_tree
tree = Tree.objects.get_or_create(treeId=treeId)[0]
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 424, in get_or_create
return self._create_object_from_params(lookup, params)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 465, in _create_object_from_params
six.reraise(*exc_info)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 457, in _create_object_from_params
obj.save(force_insert=True, using=self.db)
File "/cygdrive/c/users/user/workspace/hug/timetravellingtrout/hug_project/hug/models.py", line 30, in save
super(Tree, self).save(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 589, in save
force_update=force_update, update_fields=update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 698, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 731, in _do_insert
using=using, raw=raw)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: hug_tree.streetNumber
I have no clue what's causing it and I've tried all the possible solutions I've found about this error. I've tried using null=True
, blank=True
, migrating the stuff, but nothing is working.
My other files look like this: models.py
class Tree(models.Model):
treeId = models.IntegerField(primary_key=True)
neighbourhood = models.CharField(max_length=128)
commonName = models.CharField(max_length=128)
diameter = models.FloatField(null=True, blank=True)
streetNumber = models.PositiveSmallIntegerField()
street = models.CharField(max_length=128)
def save(self, *args, **kwargs):
self.slug = slugify(self.treeId)
super(Tree, self).save(*args, **kwargs)
def __unicode__(self):
return str(self.treeId)
script
def populate():
parsedfiles = minidom.parse('C:\Users\User\Downloads\New folder\StreetTrees_ArbutusRidge.xml')
treelist = parsedfiles.getElementsByTagName('StreetTree')
for alltrees in treelist:
treeId = alltrees.getAttribute('TreeID')
neighbourhood = alltrees.getElementsByTagName('NeighbourhoodName')
commonName = alltrees.getElementsByTagName('CommonName')
diameter = alltrees.getElementsByTagName('Diameter')
streetNumber = alltrees.getElementsByTagName('CivicNumber')
street = alltrees.getElementsByTagName('StdStreet')
add_tree(treeId=treeId, neighbourhood=neighbourhood, commonName=commonName,
diameter=diameter, streetNumber=streetNumber, street=street)
def add_tree(treeId, neighbourhood, commonName, diameter, streetNumber, street):
tree = Tree.objects.get_or_create(treeId=treeId)[0]
tree.neighbourhood = neighbourhood
tree.commonName = commonName
tree.diameter = diameter
tree.streetNumber = streetNumber
tree.street = street
tree.save()
return tree
# Start execution here!
if __name__ == '__main__':
print "Starting update script..."
populate()
getElementsByTagName()
returns a list of elements. Your field only accepts a positive small integer (not a list). You should change the following line to target a single element:You will likely have to do this for your other fields as well...