Python-based CSV ORM
By Justin

Let's use a Python-based CSV ORM to learn about Python and SQL.
Why? Python is not SQL. That's probably obvious.
An ORM (object relation mapper) is a way that developers can write Python code that gets converted into database calls (queries).
This is a basic example using pure Python and storing/retrieving data in a comma separated values (csv) file.
Here's how you use it:
- Install Python 3.8+ (tested on Python 3.13) (learn how on this in-depth python installation course or this simple step-by-step guide)
- Download csv_orm.py to your computer from this github gist
- Next to csv_orm.py, create a file called mydata.py. This file will hold our code.
Ensure that you can run python3 mydata.py and python3 csv_orm.py and see no errors.
Now, in mydata.py, we'll create a database model by using the Python Class Model written in csv_orm.py:
python
from csv_orm import Model
class Person(Model):
pass
Right now, this model does not have any fields associated to it. Fields will be converted into the columns in the csv file.
Let's add a few fields:
- CharField or Character Field: mix letters and numbers and set a maximum length
- IntegerField: whole numbers only please
- DateTimeField: A field that stores timestamps for us (e.g. datetime.now())
These fields are also defined in csv_orm.py.
Open mydata.py and add:
python
from csv_orm import Model, CharField, IntegerField, DateTimeField
class Person(Model):
name = CharField(max_length=100)
age = IntegerField()
created_at = DateTimeField(auto_now_add=True)
updated_at = DateTimeField(auto_now=True)
We now have a model ready to use. Open up your terminal/command line and run:
python3 -i mydata.py
Using -i means we use this file in intactive mode so we do not need to import anything
Now let's see how simple it is to save some data:
python
Person.objects.create(
name="Justin",
age=103
)
If done correctly, you should be able to open up the folder db and see person.csv with 1 row in there:
csv
name,age,created_at,updated_at,id
Justin,103,2025-07-30T11:28:46.463350,2025-07-30T11:28:46.464390,091594d9-a539-4efa-851f-5669584bbffd
You should now notice 5 total columns:
- name (defined by us)
- age (defined by us)
- created_at(defined by us, auto set)
- updated_at (defined by us, auto set)
- id (not defined by us, auto set)
Exit the python terminal:
>>> exit()
Re-enter the mydata.py file:
python3 -i mydata.py
Create a new person entry and set it to a variable
python
bob = Person.objects.create(
name="Bob",
age="99",
)
Now change bob's age:
python
bob.age = 107
If you want, you can check the db/person.csv file to see what is stored as Bob's age. It should be 99 and not 107. Let's change that:
python
bob.save()
Now if you open _db/person.csv_ you should see two changes for Bob:
- age should be 107 now.
- updated_at should not be the same as created_at
Naturally we could add a lot more data to the CSV file but we'll run into a major issue if we do: csv files are not databases.
Databases are optimized to handle huge amounts of data, csv files are not.
In other words, we're now ready to use Django ORM Models to store data directly to: SQLite, PostgreSQL, MySQL, MariaDB, and so on.
While Django models can get a lot more complex than this, you now have the fundamentals down before ever installing or setting up Django.
Challenge: how would you design a model for storing blog posts?
