Make random PK mixin work with pre_save signals
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -11,8 +11,8 @@ from django.conf import settings
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.db import connection
|
from django.db import connection. models
|
||||||
from django.db import models
|
from django.db.models.signals import post_save, pre_save
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template import Context, Template
|
from django.template import Context, Template
|
||||||
from django.test.utils import ContextList
|
from django.test.utils import ContextList
|
||||||
@@ -176,12 +176,35 @@ class RandomPrimaryKeyModelMonkeyPatchMixin(object):
|
|||||||
if instance.pk:
|
if instance.pk:
|
||||||
return self.method_save_original(instance, *args, **kwargs)
|
return self.method_save_original(instance, *args, **kwargs)
|
||||||
else:
|
else:
|
||||||
|
# Set meta.auto_created to True to have the original save_base
|
||||||
|
# not send the pre_save signal which would normally send
|
||||||
|
# the instance without a primary key. Since we assign a random
|
||||||
|
# primary key any pre_save signal handler that relies on an
|
||||||
|
# empty primary key will fail.
|
||||||
|
# The meta.auto_created and manual pre_save sending emulates
|
||||||
|
# the original behavior. Since meta.auto_created also disables
|
||||||
|
# the post_save signal we must also send it ourselves.
|
||||||
|
# This hack work with Django 1.11 .save_base() but can break
|
||||||
|
# in future versions if that method is updated.
|
||||||
|
pre_save.send(
|
||||||
|
sender=instance.__class__, instance=instance, raw=False,
|
||||||
|
update_fields=None,
|
||||||
|
)
|
||||||
|
instance._meta.auto_created = True
|
||||||
instance.pk = RandomPrimaryKeyModelMonkeyPatchMixin.get_unique_primary_key(
|
instance.pk = RandomPrimaryKeyModelMonkeyPatchMixin.get_unique_primary_key(
|
||||||
model=instance._meta.model
|
model=instance._meta.model
|
||||||
)
|
)
|
||||||
instance.id = instance.pk
|
instance.id = instance.pk
|
||||||
|
|
||||||
return instance.save_base(force_insert=True)
|
result = instance.save_base(force_insert=True)
|
||||||
|
instance._meta.auto_created = False
|
||||||
|
|
||||||
|
post_save.send(
|
||||||
|
sender=instance.__class__, instance=instance, created=True,
|
||||||
|
update_fields=None, raw=False
|
||||||
|
)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
setattr(models.Model, 'save', method_save_new)
|
setattr(models.Model, 'save', method_save_new)
|
||||||
super(RandomPrimaryKeyModelMonkeyPatchMixin, self).setUp()
|
super(RandomPrimaryKeyModelMonkeyPatchMixin, self).setUp()
|
||||||
|
|||||||
Reference in New Issue
Block a user