Home:ALL Converter>django-simple-history save_without_historical_record is not working

django-simple-history save_without_historical_record is not working

Ask Time:2018-09-12T14:23:20         Author:Pooja G

Json Formatter

The following is my django model Banner:

class Banner(models.Model):

    id = models.AutoField(primary_key=True)
    description = tinymce_models.HTMLField()
    created_time = models.DateTimeField(auto_now=True)
    modified_time = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(default=False)
    history = HistoricalRecords()


    def save_without_historical_record(self, *args, **kwargs):
        self.skip_history_when_saving = True
        try:
            ret = self.save(*args, **kwargs)
        finally:
            del self.skip_history_when_saving
        return ret

    class Meta:
        db_table = 'banners'
        managed = False

Nose test case which access the above model:

import unittest
from tests import factory as f

class BannerTest(unittest.TestCase):

    def test_insert_to_banner(self):
        """
        Banner test insert
        """
        f.banners(description="sample 
 banner123").save_without_historical_record()

factory :

def banners(**kwargs):
    print kwargs
    return _create_default_model_instance(test_app.Banner, **kwargs)

When this test case is executed, it is not saving without historical record, as it is expecting a User instance.

Complete traceback:

Traceback (most recent call last):
  File "/service/webapps/build/tests/test_banners.py", line 12, in test_insert_to_banner
    f.banners(description="sample banner123", save=True)
  File "/service/webapps/build/tests/factory.py", line 320, in banners
    return _create_default_model_instance(triage_models.Banner, **kwargs)
  File "/service/webapps/build/tests/factory.py", line 35, in saving_func
    model_instance.save()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 565, in save_base
    created=(not record_exists), raw=raw, using=using)
  File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/service/webapps/external_apps/simple_history/models.py", line 214, in post_save
    self.create_historical_record(instance, created and '+' or '~')
  File "/service/webapps/external_apps/simple_history/models.py", line 227, in create_historical_record
    history_user=history_user, **attrs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 137, in create
    return self.get_query_set().create(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 375, in create
    obj = self.model(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 355, in __init__
    setattr(self, field.name, rel_obj)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 366, in __set__
    self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "<django.utils.functional.SimpleLazyObject object at 0x7fc74dce5dd0>": "HistoricalBanner.history_user" must be a "User" instance.

Problem seems to be in post_save function in simple_history/models.py. created flag is sent True which in turn will not make first condition to return.

def post_save(self, instance, created, **kwargs):
            if not created and hasattr(instance, 'skip_history_when_saving'):
                return
            if not kwargs.get('raw', False):
                self.create_historical_record(instance, created and '+' or '~')

This created flag is set from django models script:

File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 565, in save_base
created=(not record_exists), raw=raw, using=using)

Not sure about what to be done.

Author:Pooja G,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52288697/django-simple-history-save-without-historical-record-is-not-working
yy