Andy McKay

Jan 05, 2008

Using profiles with Django

There's a feature in Django to add in new values to a user record, use a profile. Here's an example: So first thing I did was write a unit test for it, which failed.

This got me confused for a minute because I thought that when you created a user, it created the profile for you automatically. Duh! So for the record here's a simple unit test, create a user, set the company (a field on the Profile object), retrieve it and check the company is still there.

import unittest
from models import Profile
from django.contrib.auth.models import User
from django.contrib import auth

class TestSecurity(unittest.TestCase):
    def setUp(self):
        user = User.objects.create_user(username="bob",
        profile = Profile.objects.create( = "Test"

    def testProfile(self):
        user = User.objects.get(username="bob")
        assert user.get_profile().company == "Test"