Kaydet (Commit) 290c9d66 authored tarafından Abhaya Agarwal's avatar Abhaya Agarwal Kaydeden (comit) Tim Graham

[1.8.x] Fixed #24698, #24712 -- Added ForeignKey.get_db_prep_value()

Fixed crashes with ForeignKey to UUIDField and inheritance with UUIDField
primary keys.
üst 3ad1074c
...@@ -1939,6 +1939,9 @@ class ForeignKey(ForeignObject): ...@@ -1939,6 +1939,9 @@ class ForeignKey(ForeignObject):
else: else:
return self.related_field.get_db_prep_save(value, connection=connection) return self.related_field.get_db_prep_save(value, connection=connection)
def get_db_prep_value(self, value, connection, prepared=False):
return self.related_field.get_db_prep_value(value, connection, prepared)
def value_to_string(self, obj): def value_to_string(self, obj):
if not obj: if not obj:
# In required many-to-one fields with only one available choice, # In required many-to-one fields with only one available choice,
......
...@@ -20,3 +20,8 @@ Bugfixes ...@@ -20,3 +20,8 @@ Bugfixes
* Fixed incorrect GROUP BY clause generation on MySQL when the query's model * Fixed incorrect GROUP BY clause generation on MySQL when the query's model
has a self-referential foreign key (:ticket:`24748`). has a self-referential foreign key (:ticket:`24748`).
* Implemented ``ForeignKey.get_db_prep_value()`` so that ``ForeignKey``\s
pointing to :class:`~django.db.models.UUIDField` and inheritance on models
with ``UUIDField`` primary keys work correctly (:ticket:`24698`,
:ticket:`24712`).
...@@ -375,3 +375,11 @@ class PrimaryKeyUUIDModel(models.Model): ...@@ -375,3 +375,11 @@ class PrimaryKeyUUIDModel(models.Model):
class RelatedToUUIDModel(models.Model): class RelatedToUUIDModel(models.Model):
uuid_fk = models.ForeignKey('PrimaryKeyUUIDModel') uuid_fk = models.ForeignKey('PrimaryKeyUUIDModel')
class UUIDChild(PrimaryKeyUUIDModel):
pass
class UUIDGrandchild(UUIDChild):
pass
...@@ -6,7 +6,8 @@ from django.db import models ...@@ -6,7 +6,8 @@ from django.db import models
from django.test import TestCase from django.test import TestCase
from .models import ( from .models import (
NullableUUIDModel, PrimaryKeyUUIDModel, RelatedToUUIDModel, UUIDModel, NullableUUIDModel, PrimaryKeyUUIDModel, RelatedToUUIDModel, UUIDGrandchild,
UUIDModel,
) )
...@@ -146,3 +147,7 @@ class TestAsPrimaryKey(TestCase): ...@@ -146,3 +147,7 @@ class TestAsPrimaryKey(TestCase):
RelatedToUUIDModel.objects.update(uuid_fk=u2.pk) RelatedToUUIDModel.objects.update(uuid_fk=u2.pk)
r.refresh_from_db() r.refresh_from_db()
self.assertEqual(r.uuid_fk, u2) self.assertEqual(r.uuid_fk, u2)
def test_two_level_foreign_keys(self):
# exercises ForeignKey.get_db_prep_value()
UUIDGrandchild().save()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment