ced*_*rik 6 django-rest-framework django-simple-history
我正在使用 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": "~",
"history_user": 1
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
经过一番搜索,没有找到解决方案,我终于自己找到了。但是如果有人有更好的解决方案......
我知道已经一年了,但无论如何,也许有人觉得它很有用。这是我的解决方案(对我来说似乎容易得多):
一个新的序列化器字段:
class HistoricalRecordField(serializers.ListField):
child = serializers.DictField()
def to_representation(self, data):
return super().to_representation(data.values())
Run Code Online (Sandbox Code Playgroud)
现在只需将其用作序列化程序中的 aa 字段:
history = HistoricalRecordField(read_only=True)
Run Code Online (Sandbox Code Playgroud)
这利用了 DRF 的内置list和dict序列化器,唯一的技巧是将正确的可迭代对象传递给它,这是通过调用.values()简单历史模型管理器类来完成的。
这是我的解决方案。在serializers.py 中:
from rest_framework import serializers
from .models import Product
class sHistory(serializers.ModelSerializer):
def __init__(self, model, *args, fields='__all__', **kwargs):
self.Meta.model = model
self.Meta.fields = fields
super().__init__()
class Meta:
pass
class sProduct(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
history = serializers.SerializerMethodField()
def get_history(self, obj):
model = obj.history.__dict__['model']
fields = ['history_id', ]
serializer = sHistory(model, obj.history.all().order_by('history_date'), fields=fields, many=True)
serializer.is_valid()
return serializer.data
Run Code Online (Sandbox Code Playgroud)
有用 !我为此感到非常自豪!有什么建议 ?
| 归档时间: |
|
| 查看次数: |
3447 次 |
| 最近记录: |