From a99a3b5bfc6703f873bb3084577930573c6b0ece Mon Sep 17 00:00:00 2001 From: Bert Constantin Date: Tue, 2 Feb 2010 08:40:56 +0100 Subject: [PATCH] fix remaining potential accessor name clashes (but this only works with Django 1.2+, for 1.1 no changes). Thanks to Andrew Ingram. --- DOCS.rst | 15 ++++++++++----- pexp/models.py | 10 ++++++++++ polymorphic/polymorphic.py | 14 ++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/DOCS.rst b/DOCS.rst index 02ca4e7..45f5a12 100644 --- a/DOCS.rst +++ b/DOCS.rst @@ -379,11 +379,16 @@ Currently Unsupported Queryset Methods Restrictions & Caveats ---------------------- -* ``Django 1.1 only``: When ContentType is used in models, Django's - seralisation or fixtures cannot be used. This issue seems to be - resolved for Django 1.2 (changeset 11863: Fixed #7052, - Added support for natural keys in serialization). - +* Django 1.1 only - the names of polymorphic models must be unique + in the whole project, even if they are in two different apps. + This results from a restriction in the Django 1.1 "related_name" + option (fixed in Django 1.2). + +* Django 1.1 only - when ContentType is used in models, Django's + seralisation or fixtures cannot be used. This issue seems to be + resolved for Django 1.2 (changeset 11863: Fixed #7052, Added support + for natural keys in serialization). + + http://code.djangoproject.com/ticket/7052 + http://stackoverflow.com/questions/853796/problems-with-contenttypes-when-loading-a-fixture-in-django diff --git a/pexp/models.py b/pexp/models.py index b7e5879..aa6c7f8 100644 --- a/pexp/models.py +++ b/pexp/models.py @@ -24,3 +24,13 @@ class SModelB(SModelA): class SModelC(SModelB): field3 = models.CharField(max_length=10) +# for Django 1.2+, test models with same names in different apps +# (the other models with identical names are in polymorphic/tests.py) +from django import VERSION as django_VERSION +if not (django_VERSION[0]<=1 and django_VERSION[1]<=1): + class Model2A(PolymorphicModel): + field1 = models.CharField(max_length=10) + class Model2B(Model2A): + field2 = models.CharField(max_length=10) + class Model2C(Model2B): + field3 = models.CharField(max_length=10) diff --git a/polymorphic/polymorphic.py b/polymorphic/polymorphic.py index 186e44b..770dbd2 100644 --- a/polymorphic/polymorphic.py +++ b/polymorphic/polymorphic.py @@ -530,6 +530,8 @@ class PolymorphicModelBase(ModelBase): ################################################################################### ### PolymorphicModel +from django import VERSION as django_VERSION + class PolymorphicModel(models.Model): """ Abstract base class that provides polymorphic behaviour @@ -556,10 +558,14 @@ class PolymorphicModel(models.Model): class Meta: abstract = True - # TODO: %(class)s alone is not really enough, we also need to include app_label - patch for Django needed? - # see: django/db/models/fields/related.py/RelatedField - polymorphic_ctype = models.ForeignKey(ContentType, - null=True, editable=False, related_name='polymorphic_%(class)s_set') + # avoid ContentType related field accessor clash (an error emitted by model validation) + # we really should use both app_label and model name, but this is only possible since Django 1.2 + if django_VERSION[0] <= 1 and django_VERSION[1] <= 1: + p_related_name_template = 'polymorphic_%(class)s_set' + else: + p_related_name_template = 'polymorphic_%(app_label)s.%(class)s_set' + polymorphic_ctype = models.ForeignKey(ContentType, null=True, editable=False, + related_name=p_related_name_template) # some applications want to know the name of the fields that are added to its models polymorphic_internal_model_fields = [ 'polymorphic_ctype' ]