Repository: django-extensions/django-extensions

Issue:
UUIDField fix with inheritance
Error:
class A(models.Model):
    uuid = UUIDField()

class B(A):
    pass

b = B.objetcs.create(uuid='0')

I get: b != '0' !!!!

I can't assing an uuid. This is my fix:

```
 def pre_save(self, model_instance, add):
```
-        if self.auto and add:
-        value = super(UUIDField, self).pre_save(model_instance, add)
-        if self.auto and add and value is None:
           value = unicode(self.create_uuid())
           setattr(model_instance, self.attname, value)
           return value
       else:
-            value = super(UUIDField, self).pre_save(model_instance, add)
           if self.auto and not value:
               value = unicode(self.create_uuid())
               setattr(model_instance, self.attname, value)



Discussion:

Resolving patch:
diff --git a/django_extensions/db/fields/__init__.py b/django_extensions/db/fields/__init__.py
--- a/django_extensions/db/fields/__init__.py
+++ b/django_extensions/db/fields/__init__.py
@@ -254,12 +254,12 @@ def create_uuid(self):
             raise UUIDVersionError("UUID version %s is not valid." % self.version)
 
     def pre_save(self, model_instance, add):
-        if self.auto and add:
+        value = super(UUIDField, self).pre_save(model_instance, add)
+        if self.auto and add and value is None:
             value = unicode(self.create_uuid())
             setattr(model_instance, self.attname, value)
             return value
         else:
-            value = super(UUIDField, self).pre_save(model_instance, add)
             if self.auto and not value:
                 value = unicode(self.create_uuid())
                 setattr(model_instance, self.attname, value)


Test patch:
diff --git a/django_extensions/tests/__init__.py b/django_extensions/tests/__init__.py
--- a/django_extensions/tests/__init__.py
+++ b/django_extensions/tests/__init__.py
@@ -2,6 +2,7 @@
 from django_extensions.tests.utils import UTILS_TRUNCATE_LETTERS_TESTS
 from django_extensions.tests.utils import UTILS_UUID_TESTS
 from django_extensions.tests.json_field import JsonFieldTest
+from django_extensions.tests.uuid_field import UUIDFieldTest
 try:
     from django_extensions.tests.encrypted_fields import EncryptedFieldsTestCase
     from django_extensions.tests.models import Secret
diff --git a/django_extensions/tests/uuid_field.py b/django_extensions/tests/uuid_field.py
new file mode 100644
--- /dev/null
+++ b/django_extensions/tests/uuid_field.py
@@ -0,0 +1,52 @@
+import unittest
+
+
+from django.db import connection
+from django.conf import settings
+from django.core.management import call_command
+from django.db.models import loading
+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)
+
+class TestAgregateModel(TestModel_pk):
+    a = models.IntegerField()
+
+class TestManyToManyModel(TestModel_pk):
+    many = models.ManyToManyField(TestModel_field)
+
+class UUIDFieldTest(unittest.TestCase):
+
+    def setUp(self):
+        self.old_installed_apps = settings.INSTALLED_APPS
+        settings.INSTALLED_APPS.append('django_extensions.tests')
+        loading.cache.loaded = False
+        call_command('syncdb', verbosity=0)
+
+    def tearDown(self):
+        settings.INSTALLED_APPS = self.old_installed_apps
+
+    def testUUIDFieldCreate(self):
+        j = TestModel_field.objects.create(a=6, uuid_field=u'550e8400-e29b-41d4-a716-446655440000')
+        self.assertEquals(j.uuid_field, u'550e8400-e29b-41d4-a716-446655440000')
+
+    def testUUIDField_pkCreate(self):
+        j = TestModel_pk.objects.create(uuid_field=u'550e8400-e29b-41d4-a716-446655440000')
+        self.assertEquals(j.uuid_field, u'550e8400-e29b-41d4-a716-446655440000')
+        self.assertEquals(j.pk, u'550e8400-e29b-41d4-a716-446655440000')
+
+    def testUUIDField_pkAgregateCreate(self):
+        j = TestAgregateModel.objects.create(a=6)
+
+    def testUUIDFieldManyToManyCreate(self):
+        j = TestManyToManyModel.objects.create(uuid_field=u'550e8400-e29b-41d4-a716-446655440010')
+        self.assertEquals(j.uuid_field, u'550e8400-e29b-41d4-a716-446655440010')
+        self.assertEquals(j.pk, u'550e8400-e29b-41d4-a716-446655440010')
+