python - 角色/库存/设备数据库结构 - Django/SQLite
问题描述
我正在自学 django,并且想要一个简单的设置,让用户拥有多个角色,每个角色都有自己的设备/库存。我从来没有使用过数据库,所以让关系正确是我需要帮助/指导的事情。
我的想法: AUser
可以有多个UserCharacters
,每个都引用一个基本Character
实例,以及一些BaseAttributes
and AttributeModifiers
。每一个UserCharacter
也有一个清单,由InventoryItems
它组成,例如UserCharacters
,引用一个Item
实例,并拥有BaseAttributes
和AttributeModifiers
。
我想InventoryItems
知道BaseAttributes
在AttributeModifiers
. UserCharacters
IE,如果两个UserCharacters
每个都有一个包含Item
具有相同统计数据的“剑”的库存,我可以将该剑分配InventoryItem
给两个UserCharacters
库存,然后如果其中一个发生更改,则只需创建一个新实例?
我现在拥有的模型(请滚动,因为其中一些被隐藏了)
========================================
| User
|---
| username
========================================
========================================
| AttributedObject
|---
| attributes
| modifiers
========================================
========================================
| Attribute
|---
| name
| description
========================================
========================================
| BaseAttribute
|---
| object >- ForeignKey(AttributedObject.id), related_name='attributes'
| attribute >- ForeignKey(Attribute.id)
| value
========================================
========================================
| AttributeModifier
|---
| object >- ForeignKey(AttributedObject.id), related_name='modifiers'
| attribute >- ForeignKey(Attribute.id)
| value
| method
| duration
========================================
========================================
| Item(AttributedObject)
|---
| name
| stack_size
========================================
========================================
| Character(AttributedObject)
|---
| name
| description
========================================
========================================
| UserCharacter(AttributedObject)
|---
| user >- ForeignKey(User.id), related_name='characters'
| character >- ForeignKey(Character.id)
| inventory
========================================
========================================
| InventoryItem(AttributedObject)
|---
| user_character >- ForeignKey(UserCharacter), related_name='inventory'
| item >- ForeignKey(Item)
| amount
========================================
现在没有太多代码,因为我不想在确定正确的路线后通过设置它来更改它,但我会尝试提供任何必要的东西以获得更有用的答案。
此外,我将不胜感激对我概述的现有结构的任何建议。谢谢。
解决方案
在 SQlite3 中,我只建议为InventoryItem
s 提供一个单独的表,并将InventoryItem
s 的唯一 id 放在UserCharacter
s 库存的条目中。
另外,根据InventoryItem
可以携带的 sa 字符数,您可能还需要为InventoryItem
所有字符持有的所有 s 创建一个单独的表,其中字段表示InventoryItem
and的 id UserCharacter
。
推荐阅读
- c# - 修改 d3d9.dll 以强制对所有纹理进行 AF
- r - 在R中按组获得标准差
- mongodb - MongoDb - 在查询本身中添加键以更新对象
- node.js - 尝试在部署项目之前设置 heroku buildpack
- java - java.lang.ClassNotFoundException:javax.ws.rs.ProcessingException - 我该如何解决这个问题?
- r - 导出自定义调色板
- azure-keyvault - Key Vault 中的访问控制和访问策略之间的区别
- python - {Python 3.8} setuptools 上的 Pip 命令错误
- node.js - Node JS蓝牙模块安装错误
- html - 如何让这个 flex-direction-column 布局在 Mobile Safari 中工作?