From cb906e159326b0edb8759a5e2723d76abade762f Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 20 Apr 2010 14:39:46 +0000 Subject: [PATCH] Fixed #13366 -- Corrected the field __setstate__ method to avoid a race condition when initially importing models. Thanks to Brett Hoerner for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13005 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 5 ++++- .../admin_scripts/app_with_import/__init__.py | 0 .../admin_scripts/app_with_import/models.py | 7 +++++++ tests/regressiontests/admin_scripts/tests.py | 9 +++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/regressiontests/admin_scripts/app_with_import/__init__.py create mode 100644 tests/regressiontests/admin_scripts/app_with_import/models.py diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 1dc8dfe858..298c29b83a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -143,7 +143,10 @@ class Field(object): self.__dict__.update(data) # Restore the default - self.default = self.model._meta.get_field_by_name(self.name)[0].default + try: + self.default = self.model._meta.get_field(self.name).default + except FieldDoesNotExist: + self.default = NOT_PROVIDED def to_python(self, value): """ diff --git a/tests/regressiontests/admin_scripts/app_with_import/__init__.py b/tests/regressiontests/admin_scripts/app_with_import/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/admin_scripts/app_with_import/models.py b/tests/regressiontests/admin_scripts/app_with_import/models.py new file mode 100644 index 0000000000..06ca0e8a34 --- /dev/null +++ b/tests/regressiontests/admin_scripts/app_with_import/models.py @@ -0,0 +1,7 @@ +from django.contrib.comments.models import Comment +from django.db import models + +# Regression for #13368. This is an example of a model +# that imports a class that has an abstract base class. +class CommentScore(models.Model): + comment = models.OneToOneField(Comment, primary_key=True) diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index d8c8e1e7b2..7ec2454561 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -987,6 +987,15 @@ class ManageValidate(AdminScriptTestCase): self.assertNoOutput(err) self.assertOutput(out, '0 errors found') + def test_app_with_import(self): + "manage.py validate does not raise errors when an app imports a base class that itself has an abstract base" + self.write_settings('settings.py', + apps=['admin_scripts.app_with_import', 'django.contrib.comments'], + sdict={'DEBUG': True}) + args = ['validate'] + out, err = self.run_manage(args) + self.assertNoOutput(err) + self.assertOutput(out, '0 errors found') ########################################################################## # COMMAND PROCESSING TESTS