South migration error: UUID not defined

I have a model with a CharField field with uuid4 default value:

f = models.CharField(default=uuid4, max_length=36, unique=True, blank=True) 

and this causes the following error:

Cannot create field 'f' for model 'm': name 'UUID' not defined.

running migrate! Ho, can I fix this problem? so far i tried:

  • to define a “wrapper function” in a module for uuid (ie def getUUID ())
  • to set the default value to "f" by overriding the Model constructor

... but the problem remains: (

ps. I know that I can train sub for custom fields, but I don't use custom fields in my opinion: P

+8
django django-south
source share
3 answers

I decided to define the following helper function in my module module:

 from uuid import uuid4 def generateUUID(): return str(uuid4()) 

then

 f = models.CharField(default=generateUUID, max_length=36, unique=True, editable=False) 

south will generate a migration file (migrations.0001_initial) with the generated UUID, for example:

 default='5c88ff72-def3-4842-8d48-a75bb3240bb5' 

this is pretty unfortunate ... since this line is "static", instead it should be created dynamically with a helper function ... anyway, in the django world, it seems to work as expected ... I added some records to the database and a new UUID for each of them. Then I tried to complete my first schema migration by adding a couple of fields to my model, and they were added to the database table, as expected.

+11
source share

You can also import UUIDs into your migration:

 from uuid import UUID 
+4
source share

I just deleted the uuid directory from the 'node_modules' directory.

And then I reinstall uuid and it worked.

I hope this helped you guys <3

0
source share

All Articles