I'm a programmer, photo bug, musician, and exercise nut. This is where I post my thoughts and links.

Random header image... Refresh for more!

Extending the Django User Object

Please note: this tutorial is for Django 1.1.1, newer versions of Django may change this behavior.

I am currently working on a application which requires that I make certain additions to the Django user object. I am needing to add attributes (including more complex things like many-to-many relationships). I have found a number of ways to do this, but found many tutorials to be a bit lacking. I figured it was as good of a time as any to jump into the fray with my own.

There are two approaches to extending the User model.

  1. Subclassing the User model and overriding the authentication middleware so that it references your new user model (not django recommended).
  2. Creating a User Profile with a foreign key relationship to User

Everything I have read shows that this is the recommended approach. Creating a user profile is not difficult but does require a few steps. Here are the steps:

1. Include the User model into your models.py file

Inside of your models.py file for your application you will first need to import the user model:

from django.contrib.auth.models import User

2. Create a UserProfile object in your models.py file

You then want to add a UserProfile object creating a foreign key relationship to User and adding whatever additional fields you may need:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    zipcode = models.IntegerField(max_length=5, blank=True, null=True)
    girlfriend = models.CharField()
    #other fields as needed.....

3. Add the profile location to the settings.py file.

In order to use the built in capability that Django provides to access this additional information (which we will cover in just a bit) you must tell django where to find the user profile by adding it to settings.py:

AUTH_PROFILE_MODULE = 'appname.UserProfile'

This is really all that you need to start using the user profile addition to User. In order to use these extra fields in your app you would access them by calling the get_profile() method on the User module. For example (a small example you could reproduce in the django python shell using a UserProfile with added field zipcode):

>>> from django.contrib.auth.models import User
>>> u = User.objects.get(id=1)
>>> u.get_profile()
<UserProfile: UserProfile object>
>>> u.get_profile().zipcode
28231

You can access these attributes and change them in the same way within your application.

Adding the UserProfile to the admin

Now I would really like for my UserProfile information to be editable in the admin alongside the normal user information. This is rather simple to do. You would use the following code:

from django.contrib import admin
from django.contrib.auth.models import User
from myproject.app.models import UserProfile

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class UserAdmin(admin.ModelAdmin):
    inlines = [UserProfileInline]

# Unregister the built in user admin and register the custom User admin with UserProfile
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

The significant point to notice in this code is that we are unregistering the built in User admin and then re-registering the User admin with the UserProfile inline. Django does not like to have two admins for the same model. This is the reason for the slight backflip.

I hope this helps when you are trying to extend the user object. Now back to coding…

February 4, 2010   2 Comments

5 New Years Resolutions…. slightly belated

So we have reached the new year (ok, maybe a few weeks after the new year) and I have a few resolutions. This past year I had only one resolution, lose weight. For the first time in my life I was successful in a new years resolution and have lost nearly 60 lbs.

So this year I decided to make a new set of resolutions and increase the challenge a bit (motivated by a episode of Random with Kevin Rose and Tim Ferris; 2010 Resolutions). I have come up with six new years resolutions, some are more realistic than others, but that is half the fun. So without further delay, here we go:

1. Start my masters degree

I have been delaying this for far to long. I have a undergraduate degree and have long desired to complete a higher level degree. I have completed the application and only need to take the dreaded GMAT to finish the process. I am setting a test date for the end of February to keep me on track. If all goes well I would start the program in the fall.

2. Learn another language

I have had the opportunity to travel quite a bit and very much enjoy spending time in other cultures. Throughout all of my travels I have never committed to learning another language. I always have wanted to know another language and regret not having that knowledge when abroad. I recently saw a blog post by Tim Ferris related to learning a language in 3 months. With this motivation I have decided to learn spanish. Spanish is the second most common language in the US and I figure I could actually get some practical use out of knowing it. My proof: I will post a video of me having a conversation with another individual by the end of the year in spanish.

3. Build my concept web app

So…. can’t talk to much about this one, except to say that I have had a idea for a web application in my head for a couple of years (yes I know forever in the web world). I have not really moved on it in that time and still have not seen a service that does what I have in mind. So this year I have decided to build the site and launch. I have taken a number of major steps to get this started and hope to have a working alpha in the next couple of months. More details to come, but not quite yet.

4. Save more money

This one is simple. I have a great life, good pay and wonderful family. I pretty much am able to do what I like and don’t have a lot of worries monetarily. That having been said, I spend to much. I want to save at least half again as much as I do now per month and do more investing (I do some now, but not to the degree that I would like).

5. Six pack abs

Tthis one is VERY vain, but true none-the-less. As I mentioned at the beginning I have lost nearly 60 pounds in the last year. I am in the best shape of my life and absolutely love exercising, working out, and eating healthy (ok, still working on the last one, but getting there). My physique has changed dramatically and I am really feeling confident with the way my body is starting to shape. The last most elusive piece of the puzzle is quite simply the abs. This area is one of the toughest to sculpt. So this year (by summer) I want to have six pack abs. Vain…maybe, attainable….definitely, will you see proof….not a chance. You will have to trust me and you can ask my wife :-) .

note: for those of you wondering how I have lost the weight and started getting in the shape I love, simple, P90X. I do not work for, nor am I paid to endorse this program, it honestly has just plain worked for me. I absolutely love it and will likely write about it in a later blog post. Find the program that works great for you and do it, if you want to know more about P90X visit www.p90x.com. I can also vouch for Zumba, my wife has been using the program for about 3 months and absolutely loves it.

6. Help others more

I am very active in my church and participate in our food ministry, music program, youth program and international missions program. This year I will be going overseas to assist with a humanitarian aid project. I still want to do more! There are a lot of people around the world struggling and just plain suffering. I have been so blessed to live in a great country, have a great family, have awesome friends and a good job. I want to give back any way that I can. There are so many ways you can help, please get involved.

Well I hope you enjoyed my list, I’ll keep you up-to-date on the progress. Wish me Luck!

January 28, 2010   No Comments

10 Recommended Droid Apps

I have now been using the Google Droid for three weeks and have made a few customizations and added a couple of apps I would recommend.  Here is my list, as I find more I will update with a new post (all of these apps can be searched by name on the Android app store and are free unless otherwise noted):

1. Facebook

This one goes without saying.  We live in a world where Facebook is everywhere.  I keep up with friends, groups, aquaintances, work, almost everything on facebook.  How could you have a smart mobile phone without facebook?

2. Google Voice

I have been a Google voice user for a while but really didn’t appreciate the service until I had a Droid.  The seamless integration between voice and droid makes it a must have.  I will warn that the setup can take a few minutes and that you must make a small setting change to get all your text messaging working correctly, but it is worth it!  All of my calls now go through Google voice, and I don’t think I will ever turn back.

3. Silent Time

This great app gives you the ability to set times that the phone will go silent automatically (or loud if you prefer).  My wife loves this app because it allows me to shut my phone up at night (I also have an iPhone I use for the alarm clock).

4. CalWidget

This widget will display a listing view of your calendar appointments (both exchange calendars and personal / google calendars) on one of your home screens.  It has many custom sizes and is quite easy to configure and use.  It provides a very nice quick glance of your calendar without having to open the entire calendar application.

5. Seesmic

A very nice and small twitter client with strong and reliable features (if you have used Seesmic on the desktop then you will have a familiar experience).  This is kind of like the facebook client, how can you possibly go without some type of twitter client.

6. Gmail Unread Count

This allows you to replace the gmail icon with one that will show the number of unread messages.  Strangely both the exchange/imap/pop mail client and the built in gmail client do not show the unread count on the home screen icon.  This fixes that problem.

7. TaskPanel

This application allows you to view all actively running applications (both foreground and background) and close upon request.  It also allows you to configure when background applications will automatically close (like every time the phone goes to sleep).  This is a huge battery saver and helps the overall operational speed of the phone.

8. Weather Widgets

If you have used or played with the HTC Hero or Droid Eris then you have not doubt marveled at the polish of the UI and primarily the nifty looking clock / weather widget on the main home screen.  This app can also determine where you are located so that it may customize the weather to your local.  This widget provides that same clock mechanism and weather connection, it is by far one of my favorites (note: this one costs $1.50).

9. The Weather Channel

I like to know the weather, this one does it pretty well.  You are probably wondering why I have two weather widgets. I like to exercise and I like to play golf, unlike the Weather Widgets (which just provides overall weather for each day) this app will give you hour-by-hour, radar maps, 36 hour forecast and a bit more detail.

10. Pandora

Even with the new 40 hour a month restriction (unless you pay), pandora is the best online radio available.  This app provides the ability to listen to pandora on the run.  BIG NOTE:  Verizon data is not truly unlimited, it is capped at 5gigs before you start getting pinged with charges, be careful when using a heavy data usage app like Pandora.

So there is my top 10 thus-far, I am sure I will have more to add as time goes on.  Feel free to post your recommended apps.  Coming soon! A comparison of the Droid vs iPhone from someone who uses both every day.

January 22, 2010   2 Comments

Back to Blogging

Well after a long hiatus from blogging I have decided to jump back into the fray.  I have tried a few different approaches in the past, none of which have been successful, so I have decided to get back to basics.  I am aiming for at least one post per week (maybe more if I get crazy) and keeping it at stuff that I do every day, technology, or stuff that really interests me (which will mainly be technology).

I am not going to try and be long-winded.  I’ll simply try and be honest and interesting.

So anyway, hopefully I stick with it and hopefully you like it. Now on to the first post…

January 22, 2010   No Comments