编辑
2025-06-28
Python
00

目录

一对多(多对一)
多对多

一对多(多对一)

python
class Question(models.Model): question_text = models.CharField(max_length=200) class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE)

一个问题可以多个选择,即Question一对多Choice,外键ForeignKey放置于一对多中的多位置

多对多

python
class TestPackage(UUIDModel): """ 记录每个测试使用过插件包""" plugin_appliances = models.ManyToManyField( 'TestPluginAppliance', through='TestPackagePluginApplianceRel', blank=True, related_name='test_packages' ) class TestTaskPluginAppliance(NamedModel): """ 记录每个测试任务使用过插件设备""" pass class TestPackagePluginApplianceRel(models.Model): """ 中间模型,用于记录 TestPackage 和 TestPluginAppliance 之间的多对多关系 """ test_package = models.ForeignKey(TestPackage, on_delete=models.CASCADE) test_plugin_appliance = models.ForeignKey(TestPluginAppliance, on_delete=models.CASCADE) additional_info = models.JSONField(verbose_name='附加信息', blank=True, default=dict) class Meta: unique_together = ('test_package', 'test_plugin_appliance')

一个插件包可以在很多插件设备上使用,一个插件设备通过替换,也可以支持很多插件包

本文作者:lixf6

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!