15

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

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

and this is causing the following error:

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

running the migrate commmand! Ho can I fix this issue? so far I tried:

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

...but the problem remains :(

ps. I know that I can instruct south for custom fields, but I'm not using custom fields in my opinion :P

daveoncode
  • 18,900
  • 15
  • 104
  • 159
  • Firstly, that error doesn't correspond to the code you've posted, as `uuid4` is not the same as `UUID`. Secondly, have you defined or imported those names in your module? – Daniel Roseman Feb 23 '13 at 13:58
  • uuid4 comes from uuid module! (it's printed uppercased by south somehow)... and yes, I have imported all the necessary dependencies for my model (it works fine in django) – daveoncode Feb 23 '13 at 14:01
  • Find out that variable named `UUID` and it's value. There is no way to debug UUID from above code. If there is any traceback, post that. – Bibhas Debnath Feb 23 '13 at 14:37
  • 1
    Bumped into the same issue, and took me a while to realize that uuid.uuid4() doesn't return a string, but an object of type uuid.UUID. – anttikoo Jan 24 '14 at 11:19

4 Answers4

16

I solved defining the following helper function in my model's 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 a generated UUID like:

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

this is pretty unhappy... since that string is "static", instead it must be created dynamically using the helper function... anyway in the django's world al seems working as expected... I added some records into the database and a new UUID was generated for each one. I then tried my first schema migration by adding a couple of fields to my model and they has been added to the database table as expected.

daveoncode
  • 18,900
  • 15
  • 104
  • 159
8

You can also import UUID in your migration:

from uuid import UUID
Mindscope
  • 81
  • 1
  • 2
0

I simply removed a uuid directory from 'node_modules' directory.

And then I reinstall uuid and it worked.

I hope it helped you guys <3

0

step 1 : install uuid

npm install uuid

step 2: then import it

import { v4 as uuid } from 'uuid';

step 3: Use it

id:uuid()