将数据从迭代器加载到Django模型中的问题

时间:2011-10-27 00:47:30

标签: python django django-models iterator

我有一个webscraper,它提供要输入Django模型的数据的字典对象。我写了一个简单的脚本来测试加载数据。它通过迭代字典对象并将每个键值传递到适当的模型字段(键与相应的模型字段共享它们的名称)来操作。虽然我的新模型对象接受数据,但在迭代后不会保留它。我无法弄清楚原因。

In [1]: from events.models import Event

In [2]: eventObject = Event() #New model object to hold the item.

In [3]: item = {'title': 'hellooo', 'description': 'just a test', 'eventid': '412212'} #We want to load this item into the eventObject

In [4]: for k in item.keys():
...:     eventObject.k = item[k]
...:     print "eventObject.%s: " % (k) + eventObject.k

eventObject.eventid: 412212 #eventObject received the item's data, as to be expected...
eventObject.description: just a test
eventObject.title: hellooo

In [5]: eventObject #but now eventObject is still empty!
Out[5]: <Event: >

In [6]: eventObject.title #just making sure.
Out[6]: ''

In [7]: for k in item.keys():
    eventObject.k = item[k]
    print "eventObject.%s: " % (k) + eventObject.k
    eventObject.save()  #Oh, maybe we need to save it...

eventObject.eventid: 412212
---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)

/home/anthony/Dropbox/Projects/Django/livingCityMap/<ipython console> in <module>()

/usr/local/lib/python2.7/dist-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using)
    458         if force_insert and force_update:
    459             raise ValueError("Cannot force both insert and updating in model saving.")
--> 460         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    461 
    462     save.alters_data = True

/usr/local/lib/python2.7/dist-packages/django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update, using)
    551                 if values:
    552                     # Create a new record.

--> 553                     result = manager._insert(values, return_id=update_pk, using=using)
    554                 else:
    555                     # Create a new record with defaults for everything.


/usr/local/lib/python2.7/dist-packages/django/db/models/manager.pyc in _insert(self, values, **kwargs)
    193 
    194     def _insert(self, values, **kwargs):
--> 195         return insert_query(self.model, values, **kwargs)
    196 
    197     def _update(self, values, **kwargs):

/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in insert_query(model, values, return_id, raw_values, using)
1432     part of the public API.
1433     """
1434     query = sql.InsertQuery(model)
1435     query.insert_values(values, raw_values)
-> 1436     return query.get_compiler(using=using).execute_sql(return_id)

/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, return_id)
    789     def execute_sql(self, return_id=False):
    790         self.return_id = return_id
--> 791         cursor = super(SQLInsertCompiler, self).execute_sql(None)
    792         if not (return_id and cursor):
    793             return

/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    733 
    734         cursor = self.connection.cursor()
--> 735         cursor.execute(sql, params)
    736 
    737         if not result_type:

/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
    32         start = time()
    33         try:
---> 34             return self.cursor.execute(sql, params)
    35         finally:
    36             stop = time()

/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.pyc in execute(self, query, args)
    42     def execute(self, query, args=None):
    43         try:
---> 44             return self.cursor.execute(query, args)
    45         except Database.IntegrityError, e:
    46             raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]

IntegrityError: null value in column "eventid" violates not-null constraint

这个模型,以防万一你好奇:     来自django.contrib.gis.db导入模型

class Event(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    eventid = models.IntegerField(max_length=200)
    genre = models.CharField(max_length=200)
    contact = models.CharField(max_length=200)
    cost = models.CharField(max_length=200)

    venue = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    neighborhood = models.CharField(max_length=200)
    city = models.CharField(max_length=200)


    date = models.DateTimeField()  # This will be replaced by a new class
    time = models.CharField(max_length=200) # This should obviously be datetime and probably exist in it's own class, but first requires the input to be normalized (via serious RegEx magic)
    def __unicode__(self):
        return self.title

1 个答案:

答案 0 :(得分:1)

您不能以这种方式通过变量值设置属性。 eventOjbect.k = 'hello'只是将属性k设置为模型所没有的'hello'

您只是分配eventObject.k ='foo'并反复重复。

for k in item.keys():
    setattr(eventObject, k, item[k])
    print "eventObject.%s: " % (k) + eventObject.k

或者在您的情况下,您可以将这些键/值对传递给模型构造函数

eventObject = Event(**items)