所以我试图从命令行运行一个脚本来解析本地存储的xml文件中的信息。每当我运行脚本时,我都会收到此错误。
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
我不知道是什么导致它,我已经尝试了所有可能的解决方案,我发现这个错误。我尝试使用null=True
,blank=True
来迁移这些内容,但没有任何效果。
我的其他文件如下所示: 的 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)
脚本
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()
答案 0 :(得分:0)
getElementsByTagName()
返回元素列表。您的字段仅接受正小整数(不是列表)。您应该更改以下行以定位单个元素:
streetNumber = alltrees.getElementsByTagName('CivicNumber')[0]
您可能还必须为其他领域执行此操作...