问题描述
我正在弄清楚序列化程序可以如何合并或嵌套在一起。在这个例子中,我有一个列模型(由 Column
类组成)和属于列模型的数据(由数据类
)。我的问题是我不知道如何从另一个serializer类调用ModelSerializer并传递参数(结果总是为空)。 如果我模型对于这种情况是正确的,以及如何创建所需的JSON,以便结果重用现有的序列化程序,并避免重复任何数据?
注意:在最好的情况下,数据属性应该彼此依赖,因此只有这些数据被序列化,定义为列。
models.py
class Column(models.Model):
data = models.CharField(max_length = 200)
title = models.CharField max_length = 200)
def __str __(self):
return self.order
class Data(models.Model):
doc = models。 CharField(max_length = 200)
order = models.CharField(max_length = 200)
nothing = models.CharField(max_length = 200)
def __str __(self):
ret urn self.order
期望的输出:
{
columns:[
{
data:doc,
title:Doc。
},
{
data:order,
title:Order no。
},
{
data:nothing,
title:Nothing
}
],
数据:[
{
doc:564251422,
无:0.0,
订单:56421
},
{
doc:546546545,
nothing:0.0,
order:98745
}
]
}
但是,ModelSerializer的结果是这样的:
pre>
[
{
doc:564251422,
order:56421,
nothing :0.0
},
{
doc:546546545,
order:98745,
nothing:0.0
}
]
你必须添加一个包含列
和数据
属性的模型,因为它们当前没有链接。
您的 models.py
文件:
class Table(models.Model):
pass
class Column(models.Model):
data = models.CharField(max_length = 200)
title = models.CharField(max_length = 200)
table = models.ForeignKey(Table)
def __str __(self):
return self.order
class Line(models.Model):
doc = models.CharField(max_length = 200)
order = models.CharField(max_length = 200)
nothing = models.CharField(max_length = 200 )
table = models.ForeignKey(Table)
def __str __(self):
return self.order
您的 serializer.py
文件:
#导入您的相关型号和序列号
class ColumnSerializer(serializers.ModelSerializer):
类Meta:
model =列
fields = [
'data',
'title'
]
class LineSerializer(serializers.ModelSerializer):
class Meta:
model = Line
fields = [
'doc',
'order',
'nothing'
]
class TableSerializer(serializers.ModelSerializer):
columns = ColumnSerializer (many = True)
lines = LineSerializer(many = True)
class Meta:
model = Table
fields = [
'columns'
'lines'
]
现在使用 TableSerializer
serializer来序列化和反序列化你的表
对象。
关于你的模型, code>行而不是数据
可能更合适。而
阅读了解更多信息,并了解如何支持嵌套序列化程序字段的写操作。
I'm figuring out how serializers can be merged or nested into each other. In this example I have a column model (consisting of Column
class) and data that belong to the column model (consisting of Data class
). My problems is that I don't know how to call ModelSerializer from another serializer class and pass the arguments (the result is always empty).
Can you advice me if my model is correct for this situation and how to create the desired JSON so that the result reuses existing serializers and avoids duplicating any data?
Note: in best case the data attributes should be dependent on each other, so that only those data get serialized that are defined as columns.
models.py
class Column(models.Model):
data = models.CharField(max_length=200)
title = models.CharField(max_length=200)
def __str__(self):
return self.order
class Data(models.Model):
doc = models.CharField(max_length=200)
order = models.CharField(max_length=200)
nothing = models.CharField(max_length=200)
def __str__(self):
return self.order
Desired output:
{
"columns": [
{
"data": "doc",
"title": "Doc."
},
{
"data": "order",
"title": "Order no."
},
{
"data": "nothing",
"title": "Nothing"
}
],
"data": [
{
"doc": "564251422",
"nothing": 0.0,
"order": "56421"
},
{
"doc": "546546545",
"nothing": 0.0,
"order": "98745"
}
]
}
But the result with ModelSerializer is like this:
[
{
"doc": "564251422",
"order": "56421",
"nothing": "0.0"
},
{
"doc": "546546545",
"order": "98745",
"nothing": "0.0"
}
]
You have to add a model that contains columns
and data
attributes because they are currently not linked.
your models.py
file :
class Table(models.Model):
pass
class Column(models.Model):
data = models.CharField(max_length=200)
title = models.CharField(max_length=200)
table = models.ForeignKey(Table)
def __str__(self):
return self.order
class Line(models.Model):
doc = models.CharField(max_length=200)
order = models.CharField(max_length=200)
nothing = models.CharField(max_length=200)
table = models.ForeignKey(Table)
def __str__(self):
return self.order
your serializer.py
file :
# import your related models and serializers
class ColumnSerializer(serializers.ModelSerializer):
class Meta:
model = Column
fields = [
'data',
'title'
]
class LineSerializer(serializers.ModelSerializer):
class Meta:
model = Line
fields = [
'doc',
'order',
'nothing'
]
class TableSerializer(serializers.ModelSerializer):
columns = ColumnSerializer(many=True)
lines = LineSerializer(many=True)
class Meta:
model = Table
fields = [
'columns',
'lines'
]
Now use the TableSerializer
serializer to serialize and deserialize your Table
object.
Concerning your models, Line
instead of Data
is maybe more appropriate. And
Read Django-Rest-Framework - NestedRelationships for more information and learn how to support write-operations to a nested serializer field.
这篇关于Django:将串行器嵌套在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!