I have a webscraper that delivers dictionary objects of data to be entered into the Django model. I’ve written a simple script to test loading data. It operates by iterating through the dictionary object and delivering each key value into the appropriate model field (the keys share their names with the respective model fields). While my new model object accepts the data, it isn’t retained after the iteration. I can’t figure out why.
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
And the model, just in case you’re curious:
from django.contrib.gis.db import models
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
You can not set an attribute by a variable value in that way.
eventOjbect.k = 'hello'is just setting an attribute,kto'hello'which the model doesn’t have.You are simply assigning eventObject.k = ‘foo’ and repeating that over and over.
Or in your case you could just pass those key/value pairs into the model constructor