Django Notes

0 comments

A few tips, tricks, gotcha's, and other findings from working with Django for Python.

Complex Queries

Complex queries are run in Django as "raw" queries. The following information will get you well on your way with complex queries including queries on databases other than the default in a multi-database setup.


  • Multiple databases require Django 1.2+
  • When performing raw queries, the table and column names are the actual names define in the database, not the names modeled in Django.
  • Raw query must include the primary key
  • Use db_manager('mydb') to specify other than the default database.
    Ex: Author.objects.db_manager('mydb').raw('SELECT id, name FROM library_authors WHERE ....')
  • Use cursor for even more complex queries. These can be handled by a class to simplify accessing.
  • Use 2 %'s (%%) when using % in a raw query.
    Ex: "LIKE %foo" ~~> "LIKE %%foo"

References

https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-raw-cursors-with-multiple-databases
https://docs.djangoproject.com/en/1.3/topics/db/sql/
http://osdir.com/ml/django-users/2010-01/msg01419.html
http://www.djangobook.com/en/2.0/chapter10/
https://docs.djangoproject.com/en/dev/topics/db/queries/#escaping-percent-signs-and-underscores-in-like-statements

Note the warning about passing parameters to raw,
https://docs.djangoproject.com/en/dev/topics/db/sql/#passing-parameters-into-raw

Modeling Existing Tables

When modeling existing tables by default, Django gives each model an "id" field. This can cause an unknown column error if your table does not have an id field. Resolve this by declaring another column as the primary_key.

see:
https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields

Passing Data to a Template

Troubleshooting

Caught OperationalError while rendering: (1054, "Unknown column 'myTable.id' in 'field list'")

https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
must assign a column as primary key so Django does not try to use default 'id'

Cool Quick Linux Tools

0 comments

These cool tools are awesome for tweeking your Linux workflow. Admin multiple machines at the same time, add GUI dialogs to your bash scripts, and more.

In Short: shFlags, xvkbd, xclip, Zenity, cssh, tree

Zenity Date Select Dialog
Let's start with the one utility, shFlags, that would be useful in any shell script. shFlags (Shell Flags) is a terrific library that simplifies handling of command-line flags, parameters, and arguments passed to your script. It aims to be a portable cross shell/OS alternative to getopt. In the tests I've done, shFlags rocks.

xvkbd is the X Virtual Keyboard and, as it sounds, is a virtual keyboard for the X window system. It can be useful for kiosk installations, however you can use its command-line functionality to send commands to X windows from within a script. The script can fill data in a window, nice.

xclip provides a command line interface to the X11 clipboard. Hugh? Without touching the mouse, you can copy data that can be later pasted in X. A script could process some data and put the result in the clipboard for the user. xclip's functionality does not end there, but I will leave that for you to discover.

Zenity, the key to providing GTK dialog boxes for shell scripts. Confirm decisions, get input, show progress all in a nifty GTK dialog called from within the shell script. The short examples on the website obviate how cool Zenity is.

ClusterSSH, available in most Linux repositories or downloadable from Sourceforge, allows you to ssh into and issue commands to multiple machines at the same time. Now making quick modifications on multiple clients is a breeze.

Tree. This seemingly simple command line tool is amazing for displaying your directory structure in a tree format. Visualize and analyze your directory and file structure quickly and easily.

Linux Timezone QuickTip

0 comments

Recently, after moving to a different timezone, I noticed that my automated backup scripts were failing. Why? They were running based on the old timezone (during a disallowed time).

To update to the new timezone, specify the correct timezone in the directory path below:

cd /etc

sudo ln -sf /usr/share/zoneinfo/US/[yourzone] localtime
Issue the date command to see the new date/time/zone.

Logic:
/etc/localtime is a symbolic link to the correct timezone data as stored in /usr/share/zoneinfo.  Directories are also available for other countries within the zoneinfo directory.

Google Tools (I Didn't Know I Was Missing)

0 comments

My Google search results recently returned a couple of Google tools that I hadn't seen listed yet. I occasionally browse the more | even more page in my Google account to see what's new. Here are two tools I haven't seen listed there yet (one very useful, one just for fun).

  • Google Financial Comparison
    Find and compare rates on mortgages, credit cards, CDs, checking and savings accounts.

    Includes a nice overview, side-by-side comparison, and more. And in typical Google style, all without being bombarded with obnoxious advertising like many other bank/financial rating sites.
  • Google Hotpot
    Rate and review places you know, see recommendations from friends and find new places within your profile based on the ratings and reviews you submit.

I Need Big Expensive Software! (Not Really)

0 comments

A recent issue of Database Trends and Applications magazine quoted a Forrester Research study that found 80% of database installations only use 20% of the features provided. My estimation is that this statistic also applies to other software, especially some of the most "popular" used and required on home, school, and office PCs. And this is a large aspect of why open source software should be used, encouraged, and by no means discriminated against in the public arena.

Let's avoid any anti-Windows or anti-Microsoft propaganda or personal preference. Anyone who knows me, already knows my personal preference for Ubuntu Linux anyhow. I'm going to focus briefly on the MS Office suite of applications, because as we know it is the dominant software in this arena.

My wife does things with MS Office at work that would make most people's head spin. She needs and uses the power, APIs, and scripting that it provides. She justifies the $X for her corporate license for Access, Word, Excel, etc. At home, she uses a word processor, rarely, for basic documents and a spreadsheet for rosters with name, address, etc. There is no justifying the expense of MS Office for home. She will occasionally use the old copy of Office97 from our college days, but our primary productivity software is OpenOffice.org. For me, running a Linux desktop, it's always OpenOffice or other open software.

If a corporation decides to buy a corporate license for MS Office or other proprietary productivity software, that's fine by me. When a government spends tax money on licenses for employees who don't use/need the added functionality, it's misuse of resources.

When a public school requires students to use MS Office to read an assignment downloaded from the Internet or as the only format for submitted assignments, there is a problem and an unfair bias toward students who can afford home computers and more over the MS Office software. This irks me. Of course OpenOffice can read/write to MS Office formats, so those "in the know" are OK (most of the time) but the majority of educators do not know this and if they do it is not taught or explained to students.

Teach students fundamentals, not memorized specifics, so that they can rapidly adapt and learn a variety of tools. I have seen countless times educators and students alike turn aside from alternative software options because it's "not the same," "too different," "too hard." The fundamental differences in MS Office and OpenOffice are negligible, and in my opinion, no more difficult to grasp than when MS Office dropped the menu bar and got all decorated with ribbons and bows. Users adapted to the changes due to necessity. It is no more difficult to adapt to another office suite.

The principle of cost vs features vs needed features vs compatibility, should be looked at in all organizations from small to large corporations, schools, governments, churches, and non-profits.

My point in all this is use the software you need for the job your performing and don't force others into your boat when another (potentially free) option will work equally well.

Mounting USB Storage Devices

0 comments

Today I noticed that my USB storage device (thumb drive, usb drive, memory stick; call it what you will) was not mounting when plugged into the PC. Note: I'm currently running Ubuntu 10.10 Maverick Meerkat.

The solution that fixed this was

sudo apt-get install usbmount
It's also good to note that lsusb will list usb devices detected on your system. Here you'll likely see reference to the usb drive although it isn't being mounted.

And for fun, gconf-editor lets you tweek all manner of settings, one of which is the Nautilus applications setting for auto mounting storage devices.


Note:
usbmount has been known to contain a bug that causes the device to mount as root instead of the logged in user, meaning you cannot modify the data. If this is the case usbmount may not be the correct fix for you. You can also try using Disk Utility to unmount/mount the device and see if that works.
     System | Administration | Disk Utility