注意:这并不是真正关于让Django的中间模型与多对多关系的额外字段工作。这是关于如何使用Django Rest Framework的Serializers.ModelSerializer与中间模型。 我有这些模型(以及更多):
class Method(models.Model):
name = models.CharField(max_length=50, unique=True)
descripton = models.TextField(null=False)
class Version(models.Model):
version_number = models.CharField(max_length=50)
cmd_line_script = models.CharField(max_length=250, null=False)
SOP = models.TextField(null=False)
FK_method = models.ForeignKey(Method, on_delete=models.CASCADE)
class Meta:
unique_together = ('version_number', 'FK_method')
class Instrument(models.Model):
serial_number = models.CharField(max_length=50, unique=True)
asset_number = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=50)
checksum_string = models.CharField(max_length=128, null=True)
FK_instr_type = models.ForeignKey(InstrType, related_name='installations', on_delete=models.PROTECT)
Instr_Version = models.ManyToManyField(
Version,
through='Instr_Version',
related_name = 'Instr_Version',
)
class Instr_Version(models.Model):
FK_version = models.ForeignKey(Version, on_delete=models.CASCADE)
FK_instrument = models.ForeignKey(Instrument, on_delete=models.CASCADE)
validating_user = models.ForeignKey(UserProfile, on_delete=models.PROTECT)
timestamp = models.DateField(auto_now_add=True)
class Meta:
unique_together = ('FK_version', 'FK_instrument')
他们很好。我正在尝试使用[Django-Rest-Framework serializers][1]
来序列化Instr_Version表,以便api可以检索所有版本的json表示(以及它们的FK_method数据,通过MethodSerializer),这些表示列为对该工具有效。
到目前为止,我有这些序列化程序(还有一些用于InstrType,UserProfile等)
class MethodSerializer(serializers.ModelSerializer):
class Meta:
model = Method
fields = ('id', 'name', 'description', 'version_set')
class VersionSerializer(serializers.ModelSerializer):
method = MethodSerializer(read_only=True)
#Instr_Version = Instr_VersionSerializer(source='Instr_Version_set', many=True, read_only=True)
class Meta:
model = Version
fields = ('id', 'method', 'version_number', 'cmd_line_script', 'SOP')
class Instr_to_VersionSerializer(serializers.ModelSerializer):
version = VersionSerializer(source='FK_version_id', read_only=True, many=False)
validator = UserProfileSerializer(source='validating_user', read_only=True)
class Meta:
model = Instr_Version
fields = ('id', 'version', 'validator', 'timestamp')
class InstrumentSerializer(serializers.ModelSerializer):
instr_type = InstrTypeSerializer(source='FK_instr_type', read_only=True)
Validated_Versions = Instr_to_VersionSerializer(source='Instr_Version', many=True, read_only=True)
class Meta:
model = Instrument
fields = ('id', 'asset_number', 'serial_number', 'name', 'checksum_string', 'instr_type', 'Validated_Versions')
我得到的最接近的是GET,如:“api / getInstrument /?asset_number = 1234” 结果:
{
"id": 11,
"asset_number": "1234",
"serial_number": "1234",
"name": "Instrument1",
"checksum_string": "0123456789ABCDEF0123",
"instr_type": {
"id": 70,
"make": "Make1",
"model": "Model1",
"service_email": "model1@company.com",
"service_website": "www.model1.com"
},
"Validated_Versions": [{
"id": 9
}, {
"id": 10
}, {
"id": 12
}]
}
这是非常好的,但validated_versions数组应该有比'id'更多的数据。
为了实验,我尝试在id
和Instr_to_VersionSerializer
的字段列表中添加/删除VersionSerializer
。证据表明id
中字段列表中的Instr_to_VersionSerializer
实际上是在FK_version_id
模型上打印Instr_Version
(或版本模型上的PK),而不是PK像我预期的那样Instr_Version
。这让我觉得DJR“自动”通过多对多直接看到Version
模型,所以我尝试将Instr_to_VersionSerializer
更改为:
class Instr_to_VersionSerializer(serializers.ModelSerializer):
#version = VersionSerializer(source='FK_version_id', read_only=True, many=False)
validator = UserProfileSerializer(source='validating_user', read_only=True)
#method = MethodSerializer(read_only=True)
class Meta:
model = Instr_Version
fields = ('id', 'version_number', 'cmd_line_script', 'SOP', 'validator', 'timestamp')
哪个会让VersionSerializer完全退出,但我收到Field name
版本号is not valid for model
Instr_Version .
错误,所以我不确定发生了什么。
我自己最后一次尝试是将source
的{{1}}更改为version = VersionSerializer
,这是相反的关系。这似乎不合逻辑,但我正处于那个我正在尝试任何事情的地步。结果是:versions
不是有效字段的错误,它告诉我我终于到达了version_number
,所以我在VersionSerializer
中注释了大部分字段只是为了看看是什么正在发生,但这只会导致:
VersionSerializer
答案 0 :(得分:0)
related_name
与Instruments
之间的关系Instr_Version
导致了问题。我将模型更新为:
class Instrument(models.Model):
serial_number = models.CharField(max_length=50, unique=True)
asset_number = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=50)
checksum_string = models.CharField(max_length=128, null=True)
FK_instr_type = models.ForeignKey(InstrType, related_name='installations', on_delete=models.PROTECT)
VersionsFromInstrument = models.ManyToManyField(
Version,
through='Instr_Version',
related_name = 'InstrumentsFromVersion',
)
(注意最后一个属性上的新相关名称,我更改了属性名称)和相应的序列化程序,这样做效果更好。请注意,在更新此模型后,我使用makemigrations
运行了migrate
和manage.py
,然后它正在运行。新结果是:
{
"id": 11,
"asset_number": "1234",
"serial_number": "1234",
"name": "Instrument1",
"checksum_string": "0123456789ABCDEF0123",
"instr_type": {
"id": 70,
"make": "Make1",
"model": "Model1",
"service_email": "model1@company.com",
"service_website": "www.model1.com"
},
"Validated_Versions": [{
"id": 9,
"method": {
"id": 9,
"name": "Method1",
"description": "method one description",
"version_set": [11, 9]
},
"version_name": "123",
"cmd_line_script": "script",
"SOP": "SOP"
}, {
"id": 10,
"method": {
"id": 10,
"name": "Method2",
"description": "method two description",
"version_set": [12, 10]
},
"version_name": "123",
"cmd_line_script": "script",
"SOP": "SOP"
}, {
"id": 12,
"method": {
"id": 10,
"name": "Method2",
"description": "method two description",
"version_set": [12, 10]
},
"version_name": "456",
"cmd_line_script": "script",
"SOP": "SOP"
}]
}
如果它将来对其他人有帮助,请点击我更新的Serilaizers与模型的变化相关联:
class MethodSerializer(serializers.ModelSerializer):
description = serializers.ReadOnlyField(source='descripton')
class Meta:
model = Method
fields = ('id', 'name', 'description', 'version_set')
class VersionSerializer(serializers.ModelSerializer):
method = MethodSerializer(read_only=True)
#Instr_Version = Instr_VersionSerializer(source='Instr_Version_set', many=True, read_only=True)
class Meta:
model = Version
fields = ('id', 'method')#, 'version_number', 'cmd_line_script', 'SOP')
class Instr_to_VersionSerializer(serializers.ModelSerializer):
version = VersionSerializer(source='FK_version', read_only=True, many=True)
validator = UserProfileSerializer(source='validating_user', read_only=True)
version_name = serializers.ReadOnlyField(source='version_number')
cmd_line_script = serializers.ReadOnlyField()
SOP = serializers.ReadOnlyField()
method = MethodSerializer(source='FK_method',read_only=True)
class Meta:
model = Instr_Version
fields = ('id', 'version', 'validator', 'timestamp', 'method', 'version_name', 'cmd_line_script', 'SOP')
class InstrumentSerializer(serializers.ModelSerializer):
instr_type = InstrTypeSerializer(source='FK_instr_type', read_only=True)
Validated_Versions = Instr_to_VersionSerializer(source='VersionsFromInstrument', many=True, read_only=True)
class Meta:
model = Instrument
fields = ('id', 'asset_number', 'serial_number', 'name', 'checksum_string', 'instr_type', 'Validated_Versions')
特别注意Validated_Versions =
InstrumentSerializer
行
我遗漏的一件事是,我真的希望在结果中看到validating_user
模型上的timestamp
和Instr_Version
字段,所以我仍然需要继续那。当我开始工作时,我会更新。