django - 模型设计django
问题描述
该模型如下所示。Whereapps
是一个字段,它只能具有app1
并且app2
可以作为将来增加的选择。此外,每个应用程序选择 ( app1
, app2
) 应该是一个模型,其中包含表示特征的字段。如何在 Django 模型中实现这一点。
apps:
app1:
feature_app1_1: "Data"
feature_app1_2: "Data"
app2:
feature_app2_1: "Data"
feature_app2_2: "Data"
解决方案
这听起来像你需要多对多的关系。多对多字段将限制对您创建的模型的选择,因此如果您只有 2 个应用程序,那么这些将是您唯一的选择。随着您添加更多,它们会出现。
所以,我想你想要这样的东西:
你说“应用程序”是一个字段,所以如果那是父模型上的一个字段。
Class Foo(models.Model):
apps = models.ManyToManyField(App)
Class App(models.Model):
name = models.Charfield(max_legnth=100) #App name
features = models.ManytoManyField(Feature)
Class Feature(models.Model):
name = models.Charfield(max_legnth=100) #Feature name
...... # Your data fields here.
您的父类将有一个多对多字段(“应用程序”)链接到您想要的所有应用程序,然后每个应用程序将链接到您需要的每个功能(“功能”)。然后每个特征都包含数据。
推荐阅读
- watson-conversation - 我在使用 sys-person 实体时遇到问题。它无法识别像 Hema 、 Kishan 这样的非英语名称
- rds - forefiles 批处理文件以循环通过终端服务器配置文件
- r - R删除表列中值为0的行
- javascript - submit() 重复执行&php 只接收默认值
- c# - 如何在 ASP.net 核心中为自定义模型绑定器编写单元测试
- mysql - 在 Apache Phoenix API 上运行查询时出现异常
- reactjs - 浏览器无法使用 fetch api 读取和发送 cookie,甚至将凭据设置为包含在跨源请求中
- mongodb - 如何在使用 mongoimport 将数据加载到 mongodb 时修复数据加载冗余?
- python - Python:拆分字符串,保留每个元素中的相关信息
- python - Unetstack - Python API - 组合 Tx/Rx