0

Here's a brief description of the models:

  • Model A represents a piece of equipment and has a name and a state
  • Model B represents a specific state of many model A's
  • Model B should relate to many model A's but only for a specific state of model A

Is it possible to model something like this in the datastore? I need the state of model A to be independent of any model B's, but when I peer into a model B I need to know what the state of the model A's should be that the model B is representing.

The current way I am achieving this is by making model B have string fields representing the different model A's with the name of the field being the name of the model A and the value of the field being the state the model A is supposed to be in.

This works, however it's completely static and requires manually adding fields into model B when the number of model A's change. I'm looking for a dynamic approach to solve this problem.

I hope this isn't too confusing, please ask for more clarification if it's needed.

4

1 回答 1

1

您可以为此使用参考属性:

class A(db.Model):
    state = db.ReferenceProperty(collection_name="equipment")

创建 A 时,将state属性设置为相应的B实体。

B这还会在名为的实体中创建一个属性,该属性equipment可用于获取A引用特定B.

假设您B在变量 中有一个损坏设备的实体broken。你可以通过这种方式得到所有损坏的设备

broken.equipment.get()

这也可用,ndb但细节略有不同,您需要检查文档。

于 2015-04-19T15:15:03.687 回答