Archives Jan. 26, 2012

Watch your Django field defaults do not load objects if you use South

Just ran into a South migration issue.  Basically we had a field that looked like this (simplified):

class MyModel( models.Model ):
    hostname = models.CharField(
        default = random_unique_hostname,
    )

where random_unique_hostname() was written like so (again, simplified):

def random_unique_hostname():
    test = generate_random()
    try:
        MyModel.objects.get( hostname = test )
    except MyModel.DoesNotExist, err:
        return test
    else:
        return random_unique_hostname() ...

Continue reading

Previous day

Jan. 25, 2012

Next day

Feb. 6, 2012

Archives