From 8656fffbe0d30c35a7415f8b5928d06ff93e8cc3 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 15 Jun 2009 11:54:03 +0000 Subject: [PATCH] [1.0.X] Fixed #11311 -- Reverted [10952], Refs #10785. Changeset [10952] 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 --- django/db/models/fields/related.py | 14 +++++++------- tests/modeltests/custom_pk/models.py | 13 ++++++++----- tests/regressiontests/m2m_regress/models.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index b3178bb613..c8db472867 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -132,13 +132,13 @@ class RelatedField(object): 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'): diff --git a/tests/modeltests/custom_pk/models.py b/tests/modeltests/custom_pk/models.py index b1d0cb37d0..b88af16782 100644 --- a/tests/modeltests/custom_pk/models.py +++ b/tests/modeltests/custom_pk/models.py @@ -136,11 +136,14 @@ Pass # 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 diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py index 5484b26d17..913e719902 100644 --- a/tests/regressiontests/m2m_regress/models.py +++ b/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 @@ FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name >>> sr_sibling.related.all() [] +# 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() + """ }