Skip to content

Commit

Permalink
Merge pull request #190 from tomhamiltonstubber/issue-with-generic-fo…
Browse files Browse the repository at this point in the history
…reign-keys

Raise error with GenericForeignKeys
  • Loading branch information
dmkoch authored Jun 1, 2017
2 parents 1f2da34 + b65fbfe commit 0ed1bd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions jsonfield/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django import forms
from django.core.serializers import deserialize, serialize
from django.core.serializers.base import DeserializationError
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.test import TestCase
try:
Expand All @@ -28,6 +30,18 @@ class JsonModel(models.Model):
empty_default = JSONField(default={})


class GenericForeignKeyObj(models.Model):
name = models.CharField('Foreign Obj', max_length=255, null=True)


class JSONModelWithForeignKey(models.Model):
json = JSONField(null=True)
foreign_obj = GenericForeignKey()
object_id = models.PositiveIntegerField(blank=True, null=True, db_index=True)
content_type = models.ForeignKey(ContentType, blank=True, null=True,
on_delete=models.CASCADE)


class JsonCharModel(models.Model):
json = JSONCharField(max_length=100)
default_json = JSONCharField(max_length=100, default={"check": 34})
Expand Down Expand Up @@ -59,6 +73,12 @@ class JSONModelCustomEncoders(models.Model):
)


class JSONModelWithForeignKeyTestCase(TestCase):
def test_object_create(self):
foreign_obj = GenericForeignKeyObj.objects.create(name='Brain')
JSONModelWithForeignKey.objects.create(foreign_obj=foreign_obj)


class JSONFieldTest(TestCase):
"""JSONField Wrapper Tests"""

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run(self):
from django.conf import settings
settings.configure(
DATABASES={'default': {'NAME': ':memory:', 'ENGINE': 'django.db.backends.sqlite3'}},
INSTALLED_APPS=('jsonfield',)
INSTALLED_APPS=('jsonfield', 'django.contrib.contenttypes')
)
from django.core.management import call_command
import django
Expand Down

0 comments on commit 0ed1bd7

Please sign in to comment.