Search code examples
djangodjango-modelsdjango-extensions

Django: How do I use UUIDField from django-extensions


How do I use the UUIDField in my model?

If i do

somefield = UUIDField 

i get:

 UUIDField is not defined.

I do import uuid on top of my models file.

And i have django_extensions in installed apps...


Solution

  • A good place to see how to use features of a Django app is in the app's tests.

    Here are some example models from django-extension's UUIDField tests that demonstrate how to use the field:

    from django.db import models
    from django_extensions.db.fields import UUIDField
    
    
    class TestModel_field(models.Model):
        a = models.IntegerField()
        uuid_field = UUIDField()
    
    
    class TestModel_pk(models.Model):
        uuid_field = UUIDField(primary_key=True)
    

    Without seeing the whole file, it's hard to tell what's going on. Make sure you import UUIDField from django_extensions.db.fields (like in the above example) and not Python's uuid module.