Tag: python

How To Import a CSV (or TSV) file into a Django Model

How to import a CSV file into Django

Recently I downloaded a US zip code data file in comma separated (CSV) format. Below are the steps and python script that I used to import that data into a django model for my site Wantbox.com.

The Steps to Import CSV Data into Django:

  1. Create your django model (mine is called “ZipCode”, see below).
  2. Create a python script called “load_data.py” with your correct django project and directory info (see mine below).
  3. Put the CSV file and “load_data.py” script in the same directory.
  4. From that directory run: python ./load_data.py”

That’s it. For a CSV file with about 42,000 rows this import took about 15 seconds. You can verify that the data loaded correctly by checking your django admin.

Continue reading »

Basic Python Logging in Django 1.2: Debugging Made Simple

python-logWhen I was deploying my new social shopping site Wantbox a couple of month ago, I discovered that I couldn’t use the Django print command in my live Dreamhost (Passenger WSGI) environment like this:

   print "Hello world!"

As a new site, I was still debugging a few persistent nits and really needed to print out messages when certain areas of the code were accessed. A few sites recommended that I try the Django debug toolbar, but I was having trouble getting that to work reliably in my Dreamhost setup.

I searched around a bit more and after consuming many web pages, finally discovered a dead simple solution using the built-in Python logging module.

Continue reading »

Real Time Analytics