Model Buddy for Python

A Python variant of “Model Buddy for PHP” currently in early development stages.

This project is in its early development stages.

I had two problems when working on database-driven projects:

  1. Writing SQL queries can be tedious and messy
  2. Using a complete MVC framework is beyond overkill for some of my side projects

I wanted a painless way to be able to pull a record from a table, manipulate it and then update it in the database without having to constantly re-write queries or learn a complicated framework. Having originally written a similar library for PHP, I decided to port its functionality over to Python.

Model Buddy reads the structures of your MySQL database tables to learn how it should handle data, can be easily customized to perform validation checks and best of all allows you to build simple applications fast without dreading database access.


How does this compare to Model Buddy for PHP?

Overall usage

Two different languages means two different approaches. With Model Buddy for PHP you need to extend the base model class for every database table you want to use. The Python edition allows you to simply import Model Buddy and use modelbuddy.get() to fetch a record.

Here’s an example:

person = modelbuddy.get(“person”, {“person_id”:”1″})

Notice how we simply specify a table using a string and the where-clause with a dictionary. In future revisions there will also be support for custom strings such as “id > %s,” similar to the PHP version.

Because you simply use the modelbuddy package instead of defining a new subclass for every table, this version does not yet have official support for custom validation methods. That’s hopefully to come!

BYODB: Bring Your Own Database

A downfall of the PHP version of Model Buddy created first is its lack of ability to use a different database engine. It has a class for the PDO_MySQL driver but it doesn’t do much more than create a connection.

I designed Model Buddy for Python to have the database driver generate and run the queries. Model Buddy simply gives the driver commands rather than doing everything itself.

Out of the box Model Buddy for Python supports MySQLdb but if you want to use PostgreSQL you can duplicate and modify the functions to match its syntax. The best part is you can now update the rest of Model Buddy without a worry as its core functionality comes separate.

Can I and should I use this in my project?

Model Buddy for Python is currently in early development stages. As I do more work in PHP I’m focusing more of my time an energy on Model Buddy for PHP. Right now this does support running SELECT, UPDATE and INSERT statements so if you need a super quick and dirty library to handle your database interactions then definitely, please use it. I wouldn’t recommend using this in a project for a client just yet however. You’re always welcome to contribute to the project by submitting pull requests on github

How do I get started?

Visit the project’s GitHub repository and either download the zip archive or clone the repo. Add the package to your project and use import modelbuddy

SELECT a record

To select a record you’ll use modelbuddy.get() which has two parameters. First, a string, the table name. Second is a dictionary containing the rows and values you want to match for a record.

Here’s an example:

person = modelbuddy.get(“person”, {“person_id”:”1″})

When you want to read a value from the object we’ll use its get() method which will return the value we specify.

So if we wanted to get our person’s first name…

print person.get(“firstname”) # Andrew

UPDATE a record

Using modelbuddy.get() returned an instance of modelbuddy.BaseModel so we’ll be working directly with our “person” object now. First we need to use person.set() which takes a dictionary of fields you want to set. This will only set the values in the client. To write our changes to the database we need to use person.update().

We may do something like this:


INSERT a record

This puts the last two commands together. When we call modelbuddy.get() we’ll only give it the first parameter; the table. This says, “Hey, don’t fetch anything, I want a blank model.” Model Buddy will automatically populate the object with default values defined in the database. After we have our fresh object we’ll use the set() method documented above and write it to the table using insert().

It all looks like this:

newPerson = modelbuddy.get(“person”)

Simple, right?

Keep checking back and don’t forget to watch / star the repository on GitHub for updates.

View the source, contribute to the project and or use it in your own by visiting my repository on GitHub.

Blog entries about Model Buddy for Python