Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:42:49+00:00 2026-05-26T11:42:49+00:00

I have a webscraper that delivers dictionary objects of data to be entered into

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:42:49+00:00Added an answer on May 26, 2026 at 11:42 am

    You can not set an attribute by a variable value in that way. eventOjbect.k = 'hello' is just setting an attribute, k to 'hello' which the model doesn’t have.

    You are simply assigning eventObject.k = ‘foo’ and repeating that over and over.

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

    Or in your case you could just pass those key/value pairs into the model constructor

    eventObject = Event(**items)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a photography site that I want to prevent image copying from. How can
Have run into a bug with glibc's malloc(): http://sourceware.org/bugzilla/show_bug.cgi?id=4349 and am thinking a work
I have a Python application. It has an SQLite database, full of data about
Have some dates in my local Oracle 11g database that are in this format:
Have an app that can use tts to read text messages. It can also
Have just started to get into CakePHP since a couple of weeks back. I
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
My python level is Novice. I have never written a web scraper or crawler.
Have an XML with next form: <categories someAttribute=test> <category id=1> <title></title> </category> <category id=1>
I have recently been learning Python and am dipping my hand into building a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.