我正在使用django-simple-history(1.8.1)和DRF(3.5.3)。我想獲得一個包含每個元素歷史記錄的休息服務。我們來舉個例子吧!從Django獲取簡單的歷史記錄
models.py
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField()
history = HistoricalRecords()
def __str__(self):
return self.name
那麼,什麼必須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": "~",
"history_user": 1
}
]
}
]
搜索whitout找到解決方案後,我終於找到了我自己。但如果有人有更好的解決方案...