diff --git a/django_dynamic_fixture/models_test.py b/django_dynamic_fixture/models_test.py index 5e2511a..9c48561 100644 --- a/django_dynamic_fixture/models_test.py +++ b/django_dynamic_fixture/models_test.py @@ -3,6 +3,8 @@ import django from django.conf import settings +from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType from django.db import models from django.core.exceptions import ValidationError from django.core.validators import RegexValidator @@ -386,4 +388,10 @@ class ModelForPlugins1(models.Model): class ModelForPlugins2(models.Model): json_field1 = JSONField2() except ImportError: - pass \ No newline at end of file + pass + + +class ModelWithGenericForeignKey(models.Model): + owner_type = models.ForeignKey(ContentType) + owner_id = models.PositiveIntegerField() + owner = GenericForeignKey('owner_type', 'owner_id') diff --git a/django_dynamic_fixture/tests/test_ddf.py b/django_dynamic_fixture/tests/test_ddf.py index 9a6ca83..ef52659 100644 --- a/django_dynamic_fixture/tests/test_ddf.py +++ b/django_dynamic_fixture/tests/test_ddf.py @@ -901,3 +901,15 @@ def test_avoid_common_name_field(self): instance = self.ddf.get(ModelWithCommonNames, field=6) self.assertEquals(6, instance.field) + + +class GenericForeignKeyTest(DDFTestCase): + def test_set_generic_foreign_key(self): + owner = self.ddf.get(ModelWithDefaultValues) + ctype = ContentType.objects.get_for_model(owner) + + instance = self.ddf.get(ModelWithGenericForeignKey, owner=owner) + + self.assertIsInstance(instance.owner_type, ctype.__class__) + self.assertEqual(instance.owner_type, ctype) + self.assertEqual(instance.owner, owner) diff --git a/settings.py b/settings.py index 407c69f..92c6b02 100644 --- a/settings.py +++ b/settings.py @@ -52,7 +52,9 @@ SECRET_KEY = 'ddf-secret-key' -INSTALLED_APPS = () +INSTALLED_APPS = ( + 'django.contrib.contenttypes', +) if DDF_TEST_GEODJANGO: INSTALLED_APPS += ('django.contrib.gis',)