title: Badly Formed hexadecimal uuid string error in Django fixture; json uuid conversion fails issue
body: <pre><code>File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2390, in get_db_prep_value
  value = uuid.UUID(value)
  File "/usr/lib/python2.7/uuid.py", line 134, in __init__
  raise ValueError('badly formed hexadecimal UUID string')
  ValueError: Problem installing fixture '/home/malikarumi/Projects/cannon/jamf/essell/fixtures/test22byhand.json': badly formed hexadecimal UUID string
  </code></pre>
  
  <p>I've found the following links so far:<br>
      <a href="https://github.com/dcramer/django-uuidfield/issues/40" rel="nofollow noreferrer">https://github.com/dcramer/django-uuidfield/issues/40</a></p>
  
  <p><a href="https://github.com/dcramer/django-uuidfield/commit/caae1bc4e45445a06dd11bb22da6a9f07395f78a" rel="nofollow noreferrer">https://github.com/dcramer/django-uuidfield/commit/caae1bc4e45445a06dd11bb22da6a9f07395f78a</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32445546/django-uuidfield-modelfield-causes-error-in-django-admin-badly-formed-hexadecim">Django UUIDField modelfield causes error in Django admin: badly formed hexadecimal UUID string</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32662933/django-primary-key-badly-formed-hexadecimal-uuid-string">Django Primary Key: badly formed hexadecimal UUID string</a></p>
  
  <p>I counted my uuidfield value. It is len=36, because it has dashes in it. At least the string representation I can see is that way. So I replaced it with the same alphanumeric without dashes, as suggested as a test by the bugfix, but I still got the same result.   </p>
  
  <p>I checked the model, but there is no max length on any uuid field, nor on the fk link back to the uuid. There's nothing on the fk to suggest it is, or should be limited to, chars, ints, uuids, etc.</p>
  
  <p>Then I found this: <a href="http://arthurpemberton.com/2015/04/fixing-uuid-is-not-json-serializable" rel="nofollow noreferrer">http://arthurpemberton.com/2015/04/fixing-uuid-is-not-json-serializable</a> which I hacked into /python2.7/site-packages/django/core/serializers/python.py. The blogger had put  it into models.py. But I got the same error, before realizing it was NOT coming from serializers/python.py, as it was yesterday, but from /usr/lib/python2.7/uuid.py, line 134, in <strong>init</strong>. the relevant portions of that code are:</p>
  
  <pre><code>if hex is not None:
          hex = hex.replace('urn:', '').replace('uuid:', '')
          hex = hex.strip('{}').replace('-', '')
          if len(hex) != 32:
              raise ValueError('badly formed hexadecimal UUID string')
          int = long(hex, 16)
  </code></pre>
  
  <p>Rather than try to hack more core code, given that the indication is the problem is json, not Python, I left this alone for now. </p>
  
  <p>Finally, I looked at this:</p>
  
  <p><a href="https://code.djangoproject.com/ticket/24012" rel="nofollow noreferrer">https://code.djangoproject.com/ticket/24012</a></p>
  
  <p>It is stated a couple of times here that Django's "UUIDField generates UUIDs in Python". Now here is some history. I created one row, a single instance of Model A into Django with a fixture that had no uuid and no datefield and had no issues. (The uuidfield is on an abstract model, so it is created when the object is created). I did that because I needed the uuid of that Model A instance for a fk field in Model B, which is the one I am struggling with now. I did that by copy pasting the Model A uuid into the fk field on Model B in a csv file which I then converted to json in order to use it as a fixture. </p>
  
  <ol>
  <li>Is it possible that the uuid ran into problems in this copy paste maneuver, before the conversion to json? </li>
  <li>If not, that means even though it was an acceptable Python object when it was created, going thru the json conversion messed it up, correct?</li>
  <li>If that's the case, what is a workaround?</li>
  <li>Can the Arthur Pemberton code be made to work somewhere else in this process?</li>
  <li>If I leave the uuid off, I can probably make this work, but then I have to go back and put the all the fk uuid's in manually. Is there a better solution? Maybe a bulk insert of that field alone?</li>
  <li>This may be a recurring issue for me, because I am also using Scrapy, which supports but does not require json. None of my scraped items will come with uuid, but how do I automate adding their fk's into my process in order to get them into Django?</li>
  <li>Or is all of this a good reason to forget uuids altogether?</li>
  </ol>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT/UPDATE</strong> per @rolf:</p>
  
  <p>Since I just discovered that the django shell differs more than I realized (the shell can find settings, the regular interpreter can't) I decided to run this once in each one, but the results were the same.</p>
  
  <pre><code>(cannon)malikarumi@Tetuoan2:~/Projects/cannon/jamf$ python manage.py shell
  Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
  
  
  IPython 4.0.3 -- An enhanced Interactive Python.
  
  In [1]: uuid.UUID(a82857b6-e336-4c6c-8499-47601770b39d)
  File "&lt;ipython-input-1-e282858da374&gt;", line 1
  uuid.UUID(a82857b6-e336-4c6c-8499-47601770b39d)
                             ^
  SyntaxError: invalid syntax
  
  
  In [2]: uuid.UUID(a0a69415-6627-43db-8c7a-b57d0c4cefe2)
  File "&lt;ipython-input-2-befebf1573ba&gt;", line 1
  uuid.UUID(a0a69415-6627-43db-8c7a-b57d0c4cefe2)
                             ^
  SyntaxError: invalid syntax
  
  
  In [3]: uuid.UUID(e6e11b06-ea3b-4e98-a31f-9a83447ad884)
  File "&lt;ipython-input-3-a59ea095e61a&gt;", line 1
  uuid.UUID(e6e11b06-ea3b-4e98-a31f-9a83447ad884)
                                               ^
  SyntaxError: invalid syntax
  
  
  In [4]: uuid.UUID(bd116432-65d7-4612-abfe-9a99dcaf5cad)
  File "&lt;ipython-input-4-c4a04434aa3c&gt;", line 1
  uuid.UUID(bd116432-65d7-4612-abfe-9a99dcaf5cad)
                        ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p>Now that I have posted this, I notice that even Stack Overflow treats these uuid differently, i.e., the way they are colored, if that's relevant and meaningful here.</p>
  
  <p>But now that we know this, what do we do with / about it?</p>
  
  <p><strong>2nd Update</strong></p>
  
  <p>This morning I thought, what about a uuid that had never been anywhere but in Django? So here's what I did: </p>
  
  <pre><code>In [5]: e.uuid
  Out[5]: UUID('61877565-5fe5-4175-9f2b-d24704df0b74')
  
  In [6]: uuid.UUID(61877565-5fe5-4175-9f2b-d24704df0b74)
    File "&lt;ipython-input-6-56137f5f4eb6&gt;", line 1
      uuid.UUID(61877565-5fe5-4175-9f2b-d24704df0b74)
                            ^
  SyntaxError: invalid syntax
  
  
  In [7]: uuid.UUID('61877565-5fe5-4175-9f2b-d24704df0b74')
  ---------------------------------------------------------------------------
  NameError                                 Traceback (most recent call last)
  &lt;ipython-input-7-3b4d3e5bd156&gt; in &lt;module&gt;()
  ----&gt; 1 uuid.UUID('61877565-5fe5-4175-9f2b-d24704df0b74')
  
  NameError: name 'uuid' is not defined
  </code></pre>
  
  <p>This is apparently because I left the quote around the alphanumeric, but why that would generate a uuid not defined error, instead of 'string type' or some such error is beyond me.</p>
  
  <pre><code>In [8]: uuid.UUID(61877565-5fe5-4175-9f2b-d24704df0b74)
    File "&lt;ipython-input-8-56137f5f4eb6&gt;", line 1
      uuid.UUID(61877565-5fe5-4175-9f2b-d24704df0b74)
                            ^
  SyntaxError: invalid syntax 
  </code></pre>
  
  <p>The first time I keyed in the characters by hand. I decided to repeat the test by copying and pasting, but as you can see, it made no difference. If there was something weird about the way only the 5 that the caret is pointing to was generated, we might be on to something, but if so, why do I get the same error in the same place when I typed it in by hand myself?</p>
  
  <p>This no longer seems like a json issue to me, since – as far as I know – json has never touched this uuid, unless it did somehow in the internal workings of Django.
  Instead, there is either 
  1. something wrong with the way uuid.UUID generates uuids, or 
  2. the way it generates them on my system, (Ubuntu 15.10, Django 1.9.1, Python 2.7.10) or 
  3. the way it reads and evaluates them when they come back, like in uuid.UUID() or being input outside the internal, automatic uuid generation process. </p>
  
  <p>But that also means people using uuid.UUID() to generate uuids will never know there is an issue unless they do what I did, which is try to bring them in from outside. I remember reading somewhere that all uuids are supposed to be compatible. So, unless someone here has a better insight, I think we might be up for a bug report. But is it a Python bug, a Django bug, or both?</p>