diff --git a/channels/binding/base.py b/channels/binding/base.py index 85ec720..88ae8e8 100644 --- a/channels/binding/base.py +++ b/channels/binding/base.py @@ -120,7 +120,8 @@ class Binding(object): @classmethod def pre_save_receiver(cls, instance, **kwargs): - cls.pre_change_receiver(instance, CREATE if instance.pk is None else UPDATE) + creating = instance._state.adding + cls.pre_change_receiver(instance, CREATE if creating else UPDATE) @classmethod def post_save_receiver(cls, instance, created, **kwargs): diff --git a/channels/tests/models.py b/channels/tests/models.py new file mode 100644 index 0000000..4b1da2d --- /dev/null +++ b/channels/tests/models.py @@ -0,0 +1,12 @@ +from uuid import uuid4 + +from django.db import models + + +class TestUUIDModel(models.Model): + """ + Simple model with UUIDField as primary key for tests. + """ + + id = models.UUIDField(primary_key=True, default=uuid4) + name = models.CharField(max_length=255) diff --git a/channels/tests/settings.py b/channels/tests/settings.py index 2ddf8ac..47fc407 100644 --- a/channels/tests/settings.py +++ b/channels/tests/settings.py @@ -6,7 +6,8 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.admin', 'channels', - 'channels.delay' + 'channels.delay', + 'channels.tests' ) DATABASES = { diff --git a/channels/tests/test_binding.py b/channels/tests/test_binding.py index 6b32a02..edd5126 100644 --- a/channels/tests/test_binding.py +++ b/channels/tests/test_binding.py @@ -6,7 +6,7 @@ from channels import route from channels.binding.base import CREATE, DELETE, UPDATE from channels.binding.websockets import WebsocketBinding from channels.generic.websockets import WebsocketDemultiplexer -from channels.tests import ChannelTestCase, HttpClient, apply_routes +from channels.tests import ChannelTestCase, HttpClient, apply_routes, models User = get_user_model() @@ -55,6 +55,42 @@ class TestsBinding(ChannelTestCase): received = client.receive() self.assertIsNone(received) + def test_trigger_outbound_create_non_auto_pk(self): + + class TestBinding(WebsocketBinding): + model = models.TestUUIDModel + stream = 'test' + fields = ['name'] + + @classmethod + def group_names(cls, instance): + return ["testuuidmodels"] + + def has_permission(self, user, action, pk): + return True + + client = HttpClient() + client.join_group('testuuidmodels') + + instance = models.TestUUIDModel.objects.create(name='testname') + + received = client.receive() + self.assertTrue('payload' in received) + self.assertTrue('action' in received['payload']) + self.assertTrue('data' in received['payload']) + self.assertTrue('name' in received['payload']['data']) + self.assertTrue('model' in received['payload']) + self.assertTrue('pk' in received['payload']) + + self.assertEqual(received['payload']['action'], 'create') + self.assertEqual(received['payload']['model'], 'tests.testuuidmodel') + self.assertEqual(received['payload']['pk'], str(instance.pk)) + + self.assertEqual(received['payload']['data']['name'], 'testname') + + received = client.receive() + self.assertIsNone(received) + def test_trigger_outbound_create_exclude(self): class TestBinding(WebsocketBinding): model = User