{{{
class Bar(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4)
class Bar2(models.Model):
pass
}}}
If I try to get these using a value of {} they fail differently. The model
with the int pk:
{{{
>>> Bar2.objects.all().get(pk={})
TypeError: int() argument must be a string or a number, not 'dict'
}}}
whereas:
{{{
>>> Bar.objects.all().get(pk={})
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/models/query.py", line 328, in get
num = len(clone)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/models/query.py", line 144, in __len__
self._fetch_all()
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/models/query.py", line 238, in iterator
results = compiler.execute_sql()
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-
packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
InterfaceError: Error binding parameter 0 - probably unsupported type.
}}}
This inconsistency leads to issues like: https://github.com/tomchristie
/django-rest-framework/issues/2970
--
Ticket URL: <https://code.djangoproject.com/ticket/24859>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Uncategorized => Database layer (models, ORM)
* needs_tests: => 0
* needs_docs: => 0
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
Accepting in a similar vein to #24319. Here's a test that can be used to
reproduce the issue:
{{{
#!diff
diff --git a/tests/model_fields/test_uuid.py
b/tests/model_fields/test_uuid.py
index 21c2869..95d47b5 100644
--- a/tests/model_fields/test_uuid.py
+++ b/tests/model_fields/test_uuid.py
@@ -37,6 +37,9 @@ class TestSaveLoad(TestCase):
loaded = NullableUUIDModel.objects.get()
self.assertEqual(loaded.field, None)
+ def test_pk_validated(self):
+ PrimaryKeyUUIDModel.objects.get(pk={})
+
def test_wrong_value(self):
self.assertRaisesMessage(
ValueError, 'badly formed hexadecimal UUID string',
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:1>
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:2>
* has_patch: 0 => 1
Comment:
https://github.com/django/django/pull/4758 should fix this.
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:3>
* cc: hi@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:4>
* cc: hi@… (removed)
* needs_better_patch: 0 => 1
* easy: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:5>
* cc: hi@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:6>
Comment (by cole):
OK, patch revised.
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:7>
* owner: nobody => iamsteadman
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:8>
* owner: iamsteadman =>
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:9>
* owner: => Tim Graham <timograham@…>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"20ff296cb15a53b8b9517a834688e57749b43e21" 20ff296]:
{{{
#!CommitTicketReference repository=""
revision="20ff296cb15a53b8b9517a834688e57749b43e21"
Fixed #24859 -- Made QuerySet.get() with UUIDField raise TypeError on bad
value.
For consistency with AutoField.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24859#comment:10>