首页 > 解决方案 > 角色/库存/设备数据库结构 - Django/SQLite

问题描述

我正在自学 django,并且想要一个简单的设置,让用户拥有多个角色,每个角色都有自己的设备/库存。我从来没有使用过数据库,所以让关系正确是我需要帮助/指导的事情。

我的想法: AUser可以有多个UserCharacters,每个都引用一个基本Character实例,以及一些BaseAttributesand AttributeModifiers。每一个UserCharacter也有一个清单,由InventoryItems它组成,例如UserCharacters,引用一个Item实例,并拥有BaseAttributesAttributeModifiers

我想InventoryItems知道BaseAttributesAttributeModifiers. UserCharactersIE,如果两个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
========================================

现在没有太多代码,因为我不想在确定正确的路线后通过设置它来更改它,但我会尝试提供任何必要的东西以获得更有用的答案。

此外,我将不胜感激对我概述的现有结构的任何建议。谢谢。

标签: pythondjangosqlite

解决方案


在 SQlite3 中,我只建议为InventoryItems 提供一个单独的表,并将InventoryItems 的唯一 id 放在UserCharacters 库存的条目中。

另外,根据InventoryItem可以携带的 sa 字符数,您可能还需要为InventoryItem所有字符持有的所有 s 创建一个单独的表,其中字段表示InventoryItemand的 id UserCharacter


推荐阅读