class Component(models.Model):
name = models.CharField(unique=True, max_length=255)
component_properties = models.ManyToManyField(ComponentProperty)
class ComponentProperty(models.Model):
label = models.CharField(unique=True, max_length=255)
component_type = models.CharField(max_length=255)
和序列化程序:
class ComponentSerializer(serializers.ModelSerializer):
class Meta:
model = Component
fields = ('name', 'component_properties')
depth = 1
class ComponentPropertySerializer(serializers.ModelSerializer):
class Meta:
model = ComponentProperty
fields = ('label', 'component_type')
[
{
"pk":1,
"model":"api.componentproperty",
"fields":{
"label":"label",
"component_type":"text"
}
},
{
"pk":2,
"model":"api.componentproperty",
"fields":{
"label":"description",
"component_type":"text",
}
},
{
"pk":1,
"model":"api.component",
"fields":{
"name":"text",
"component_properties":[
1,
2
]
}
}
]
干得好!但我有20个固定装置要装。我希望fixture(例如component.json)如下所示:
[
{
"pk":null,
"model":"api.component",
"fields":{
"name":"text",
"component_properties":[
{
"pk":null,
"model":"api.componentproperty",
"fields":{
"label":"label",
"component_type":"text"
}
},
{
"pk":null,
"model":"api.componentproperty",
"fields":{
"label":"description",
"component_type":"text",
}
}
]
}
}
]
here
. 请问,有谁能帮我写这些没有pk的装置和上面描述的关系吗?