Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #11311 -- Reverted [10952], Refs #10785. Changeset [109…

…52] caused problems with m2m relations between models that had non-integer primary keys. Thanks to Ronny for the report and test case.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@11008 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8656fffbe0d30c35a7415f8b5928d06ff93e8cc3 1 parent 1ff9ec9
@freakboy3742 freakboy3742 authored
View
14 django/db/models/fields/related.py
@@ -132,13 +132,13 @@ def pk_trace(value):
v, field = getattr(v, v._meta.pk.name), v._meta.pk
except AttributeError:
pass
- if not field:
- field = self.rel.get_related_field()
- if lookup_type in ('range', 'in'):
- v = [v]
- v = field.get_db_prep_lookup(lookup_type, v)
- if isinstance(v, list):
- v = v[0]
+
+ if field:
+ if lookup_type in ('range', 'in'):
+ v = [v]
+ v = field.get_db_prep_lookup(lookup_type, v)
+ if isinstance(v, list):
+ v = v[0]
return v
if hasattr(value, 'as_sql'):
View
13 tests/modeltests/custom_pk/models.py
@@ -136,11 +136,14 @@ class Foo(models.Model):
# Regression for #10785 -- Custom fields can be used for primary keys.
>>> new_bar = Bar.objects.create()
>>> new_foo = Foo.objects.create(bar=new_bar)
->>> f = Foo.objects.get(bar=new_bar.pk)
->>> f == new_foo
-True
->>> f.bar == new_bar
-True
+
+# FIXME: This still doesn't work, but will require some changes in
+# get_db_prep_lookup to fix it.
+# >>> f = Foo.objects.get(bar=new_bar.pk)
+# >>> f == new_foo
+# True
+# >>> f.bar == new_bar
+# True
>>> f = Foo.objects.get(bar=new_bar)
>>> f == new_foo
View
14 tests/regressiontests/m2m_regress/models.py
@@ -33,6 +33,14 @@ class SelfReferChild(SelfRefer):
class SelfReferChildSibling(SelfRefer):
pass
+# Many-to-Many relation between models, where one of the PK's isn't an Autofield
+class Line(models.Model):
+ name = models.CharField(max_length=100)
+
+class Worksheet(models.Model):
+ id = models.CharField(primary_key=True, max_length=100)
+ lines = models.ManyToManyField(Line, blank=True, null=True)
+
__test__ = {"regressions": """
# Multiple m2m references to the same model or a different model must be
# distinguished when accessing the relations through an instance attribute.
@@ -79,5 +87,11 @@ class SelfReferChildSibling(SelfRefer):
>>> sr_sibling.related.all()
[<SelfRefer: Hanna>]
+# Regression for #11311 - The primary key for models in a m2m relation
+# doesn't have to be an AutoField
+>>> w = Worksheet(id='abc')
+>>> w.save()
+>>> w.delete()
+
"""
}
Please sign in to comment.
Something went wrong with that request. Please try again.