以下是我的 django 模型横幅:
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
访问上述模型的鼻子测试用例:
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()
工厂 :
def banners(**kwargs):
print kwargs
return _create_default_model_instance(test_app.Banner, **kwargs)
执行此测试用例时,它不会在没有历史记录的情况下保存,因为它需要一个 User 实例。
完整的追溯:
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.
问题似乎出在 simple_history/models.py 中的 post_save 函数中。created 标志被发送 True 反过来不会使第一个条件返回。
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 '~')
这个创建的标志是从 django 模型脚本设置的:
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)
不确定要做什么。