我有一个模型预订,其中有一个历史.像这样,我使用django_simple_history
class Booking(CreatedAtAbstractBase):
history = HistoricalRecords()
Run Code Online (Sandbox Code Playgroud)
我使用管理命令来完成任务.我想在预订时预取历史记录
booking_p_history = Booking.history.filter(s_id=6).order_by(
'updated_at').first()
booking_obj_list = Booking.objects.select_related(...) \
.prefetch_related(
Prefetch('booking_history', queryset=booking_p_history, to_attr='driver_pickup_history')
,'booking_history') \
.filter(...)
Run Code Online (Sandbox Code Playgroud)
如何在预取中使用简单的历史记录?
我正在使用 django-simple-history (1.8.1) 和 DRF (3.5.3)。我想获得一个包含每个元素历史的休息服务。让我们举个例子!
模型.py
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField()
history = HistoricalRecords()
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
那么,什么必须是serializers.py?我想得到类似的东西:
[
{
"id": 1,
"name": "Apple",
"price": 8,
"history": [
{
"history_id": 1,
"id": 1,
"name": "Apple",
"price": 0,
"history_date": "2016-11-22T08:02:08.739134Z",
"history_type": "+",
"history_user": 1
},
{
"history_id": 2,
"id": 1,
"name": "Apple",
"price": 10,
"history_date": "2016-11-22T08:03:50.845634Z",
"history_type": "~",
"history_user": 1
},
{
"history_id": 3,
"id": 1,
"name": "Apple",
"price": 8,
"history_date": "2016-11-22T08:03:58.243843Z",
"history_type": "~", …
Run Code Online (Sandbox Code Playgroud) 我们django-simple-history
为我们的模型做好了准备.最近,一大堆模型被神秘地删除了.事实发生后几天就注意到了这一点,因此避免完整的数据库备份恢复会很好,因为这样可以消除事后发生的手动更改.
我找不到任何方法来轻松恢复模型实例,特别是删除的模型实例.我可以查询模型的历史版本并查找已删除的所有内容.有了这个,我还可以观察到他们所有人都有删除作为他们的最后一次改变.我可以在history-1上使用instance属性来获取删除前的状态,但是如果我尝试保存它是错误的,因为模型实例已被删除而且不再存在.
基本上,如果我有django-simple-history的历史记录,那么恢复已删除模型实例的最简洁方法是什么?如果可能的话,我想保留历史记录,所以在完全重新创建对象之前,我正在研究任何解决方案.
我正在使用django-simple-history
:
http://django-simple-history.readthedocs.io/en/latest/
我有一个模型,我想将其方法应用于历史实例。例子:
from simple_history.models import HistoricalRecords
class Person(models.Model):
firstname = models.CharField(max_length=20)
lastname = models.CharField(max_length=20)
history = HistoricalRecords()
def fullName(self):
return firstname + lastname
person = Person.objects.get(pk=1) # Person instance
for historyPerson in person.history:
historyPerson.fullName() # wont work.
Run Code Online (Sandbox Code Playgroud)
由于HistoricalPerson类没有继承Person的方法。但使用 Person 方法实际上是有意义的,因为它们共享相同的字段。
有什么解决办法吗?我更喜欢简单的东西,而不是像为历史实例复制模型中的每个方法一样。
当我从 admin.ModelAdmin 继承时,在管理页面的历史记录中,我可以看到哪些字段已更改。但是,现在我需要使用 django-simple-history 来跟踪我所有的模型更改。现在,对于管理员,我继承了 simple_history.SimpleHistoryAdmin。虽然我可以看到所有模型更改并还原它们,但我看不到更改了哪些字段。是否可以将这种方便的功能添加到 SimpleHistoryAdmin 中?
如何使用 django-simple-history 存储 ManyToManyField 的历史记录。我使用带有属性 m2m_filds 的 HistoricalRecords 但它抛出错误:意外的关键字参数 'm2m_fields'
我想为django simple-history添加管理员视图功能.我在模型上创建了一个历史属性,这个模型现在自动出现在管理文档部分,没有任何进一步的代码,但它没有出现在管理部分.我希望用户能够查看更改历史记录并使用most_recent函数应用撤消功能.你对如何处理这个问题有什么建议吗?
django-simple-history
书写历史改变instance.save()
方法。但是当我写迁移更改实例数据时,更改没有出现。
是save()
的方法
Model = apps.get_model('myapp', 'MyModel')
Run Code Online (Sandbox Code Playgroud)
和
MyModel
Run Code Online (Sandbox Code Playgroud)
一样吗?有没有办法将这种变化写入历史?
所以我正在使用django-simple-history模块,该模块跟踪Model实例中的更改。但是,它使用post_save信号来这样做。在我的项目中,我还需要它在update()上触发。
我的问题是:如何覆盖update()方法以触发post_save信号?
python django django-models django-rest-framework django-simple-history
为什么 Django-Simple 历史记录在调用 save 方法时创建,如果我调用 update 那么它不会创建历史记录?
Django:1.11.15 Django-简单历史:1.9.0 Python:3.6