Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Converting FileMaker to Django

Jerry Stratton, September 2, 2007

Around 1996 I got tired of using VI, BBEdit Lite, and Claris HomePage to create every page on my burgeoning web site. Every time I made a change to the style of the site, I had to go through each page and change the HTML surrounding the content (back then, we didn’t even have CSS).

At the time, FileMaker Pro was a great choice for databases on the Mac, and there wasn’t much of anything else. So I worked up a FileMaker database of my web site that allowed me to more easily relate folders to their contents, and to centralize common elements. AppleScripts made it easy to upload a page, a branch of the site, or all changed pages from within FileMaker to the web site via Anarchy.

That solution served me fairly well, but as time went on the important features to me in FileMaker stagnated while other more automatable and more standards-based options grew. Around the turn of the century I pretty much stopped updating the FileMaker section of the web site because it was comparatively more difficult to work with than the MySQL/PHP section of the web site that I’m writing this in.

Because of that, I haven’t upgraded my FileMaker past version 6. It is starting to show some cracks. The time has come to start migrating all of my FileMaker data before my FileMaker stops working, whether on some future OS or on an Intel Mac.

Most of that data, I’m moving into Django. It provides an easy-to-use interface to MySQL and SQLite; SQLite is a great choice for databases when I want the data in a single easily-managed file.

The goal is to be able to easily reposition my data; I want to be able to use it in PHP, to correlate it with other on-line data, to use it with any other scripting language I might need or any other web templating API. I want to be able to have scripts automatically update my data whether I'm logged in or not. FileMaker remains the best layout creator for printing, but I don’t print as often as I used to. Once the web and other Internet technologies come into play, FileMaker can be harder to use than ostensibly more difficult databases such as MySQL or SQLite.

A sample import script

Transferring from FileMaker (or some other GUI app) into SQLite or MySQL and Django, can be made much easier with appscript.

This is a simplified version of the import script that I used for importing web pages from FileMaker. This doesn’t import all of the fields in the FileMaker database, but from an example standpoint a lot of it was redundant. This is a working script, however.

This script loops through each record and imports the record into the Page model. It looks for the appropriate records from the Site and Template model and attaches them to the page. Each record (except one main home page) also has a parent record. It stores the FileMaker ID of the parent record for later, because the parent record might not have been created yet. After all records have been created, the script loops through each page and attaches its parent.

Some fields contained special codes that were replaced with common data. The text <<HELP DESK>>, for example would on publishing be replaced with contact info for help. In Django I have a library object that contains these common elements. So, fields that might contain those special codes are run through a function, fixEncodings, that replaces one with the other.

Some Django thoughts

Use Django’s development server in a separate (terminal) window. It will reload source files such as models.py and views.py as soon as you save them. This lets you easily make changes to your models and views during the conversion process.

manage.py runserver localhost:8000

http://localhost:8000/admin/

If you’re using the development server, you can “print” in models.py and views.py, and the printed text will appear in the server’s output.

Break the conversion into steps, and organize the steps into pre-conversion and post-conversion steps. Things that involve you testing the new database structure and the conversion process are pre-conversion steps. Steps that involve you modifying the data in the new database, and which you would have to manually re-enter every time you re-import, are post-conversion tasks. Keep your pre-conversion list in your import script; keep your post-conversion list in views.py.

While in the pre-conversion stage, continue editing all data in the old source, and re-import regularly.

If you have a field for keeping track of when a record was added and when it was last updated (which I strongly recommend), disable the auto_now_add and auto_now options while you’re in the pre-conversion stage. Otherwise, the Django API will overwrite what you put there from the old database with the current time.

As you edit your import script, you may notice that some things belong in separate tables. Now is a great time to make those changes.

Use Django’s admin filters to track down strange categorization data: add the fields you are currently worried about to list_filter, and then track down what categories you’re using and how many records use them.

You can construct Django admin links in models.py. Create a method for your model that returns a string, and put that method in list_display. You can use this, for example, to create your own navigational structure in the Django admin by linking to related records. For example, this will create a link that displays only the children of the current record:

Automator is a simple workflow system for Mac OS X. By its nature it is very procedural: one task follows another; workflows don’t loop and they don’t store variables for later. However, this is possible in Automator and while it adds complexities it can also solve problems such as wanting to save dropped filenames for later use.

Using a SQL database to mimic a filesystem will, eventually, create bottlenecks when it comes to traversing the filesystem. One solution is modified preordered tree traversal, which saves the tree structure in an easily-used manner inside the model.

Blogroll

Keep in touch

About Mimsy

Comments?

The undiscovered comment form, whose bourn no poster returns.

Your comment

Your name

Your email

Your web page

Your location

Your email, URL, and location are optional—but I won’t be able to contact you if you don’t leave a working email. Your email does not get displayed, your URL and location do. Your name is required but may vary as the needs of the day demand, or you can just use the anonymous Hark Thrice name. You can use the following tags: <em>, <a>, <blockquote>. Use them wisely and post intelligently. Comments may take some time to approve, especially if I’m stuck in a Mexican jail.