Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for GenericForeignKey #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion django_dynamic_fixture/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -386,4 +388,10 @@ class ModelForPlugins1(models.Model):
class ModelForPlugins2(models.Model):
json_field1 = JSONField2()
except ImportError:
pass
pass


class ModelWithGenericForeignKey(models.Model):
owner_type = models.ForeignKey(ContentType)
owner_id = models.PositiveIntegerField()
owner = GenericForeignKey('owner_type', 'owner_id')
12 changes: 12 additions & 0 deletions django_dynamic_fixture/tests/test_ddf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',)
Expand Down