Mar 12 2012

Baseball and BBQ

Today was a beautiful day.  Definitely the first day of spring as far as I’m concerned.  Sunny and 70.  We played our first baseball of the season and grilled our first steak.  A fun and delicious evening!


Mar 2 2012

Learn about UUIDs

A great post about UUIDs, the various types, and how they are calculated.


Mar 1 2012

How to add a UUID field in Django using Django Extensions and how to make it a read-only admin field

I’m using Django Extensions which is a great collection of extensions and utilities for the Django framework.  For a project I’m using it to generate and store UUID fields in several models.  I found an easy way to add that UUID field to my admin as read-only.

A sample model using Django Extensions’ UUID field (I chose to use a version 4 UUID):

 Python |  copy code |? 
1
from django.db import models
2
 
3
from django_extensions.db.fields import UUIDField
4
 
5
class SomeClass(models.Model):
6
    uuid = UUIDField(version=4)
7
    # ...other fields...

Sample admin.py class to show UUID as a read-only field:

 Python |  copy code |? 
1
from myapp.models import SomeClass
2
 
3
from django.contrib import admin
4
 
5
class SomeClassAdmin(admin.ModelAdmin):
6
    readonly_fields = ('uuid',)
7
 
8
admin.site.register(SomeClass, SomeClassAdmin)

That’s it!  Very easy to implement a UUID and make it visible as read-only in the Django admin.  Note that you need at least Django 1.2. More here.