I'd like to be able to automatically insert an entity with a reference t another entity directly from a message, using Google Endpoints.
我希望能够使用Google端点直接从邮件中自动插入带有引用另一个实体的实体。
To transmit ReferenceProperty
in message, I use the encoded string value of the Key
. That is fine for sending message, but when receiving message, and creating an entity of it, I cannot just pass the encoded string as a parameter to the constructor.
要在消息中传输ReferenceProperty,我使用Key的编码字符串值。这对于发送消息很好,但是当接收消息并创建它的实体时,我不能只将编码的字符串作为参数传递给构造函数。
For instance, say I have two classes that inherits from BaseModel
which itself inherits from db.models
例如,假设我有两个继承自BaseModel的类,它本身继承自db.models
class TestModel2(models.BaseModel):
test_string = db.StringProperty(required=True)
class TestModel(models.BaseModel):
test2 = db.ReferenceProperty(TestModel2)
test2_id = property(models.BaseModel._get_attr_id_builder('test2'),
models.BaseModel._set_attr_id_builder('test2'))
And a message class
还有一个消息类
class TestModelMessage(messages.Message):
test2_id = messages.StringField(4)
I want to be able to create an Entity
TestModel
directly of the TestModelMessage
.
我希望能够直接创建TestModelMessage的Entity TestModel。
I managed to do it in the other way (from to entity to message) using a property. But in the other way it doesn't work since I have the feeling that the constructor for db.models
will only set the attributes that inherits db.Property
. Thus the setter for the property won't be called...
我设法使用属性以另一种方式(从实体到消息)执行此操作。但另一方面它不起作用,因为我觉得db.models的构造函数只会设置继承db.Property的属性。因此,不会调用属性的setter ...
How could I do this?
我怎么能这样做?
I thought of overriding the __init__
in BaseModel
but then when calling the __init__
of db.models
it will probably override the ReferenceProperty
.
我想过覆盖BaseModel中的__init__,但是当调用db.models的__init__时,它可能会覆盖ReferenceProperty。
1 个解决方案
#1
0
So, I added a _ref_properties
field to the BaseModel
class. For the previous example, it would be _ref_properties = {'test2': 'test2_id'}
所以,我在BaseModel类中添加了一个_ref_properties字段。对于前面的示例,它将是_ref_properties = {'test2':'test2_id'}
Then I added this class method
然后我添加了这个类方法
@classmethod
def from_message(cls, message, *args):
attributes = {attr: getattr(message, attr) for attr in args}
for attribute, property in cls._ref_properties.items():
attributes[attribute] = db.Key(encoded=getattr(message, property))
entity = cls(**attributes)
return entity
And it seems to work. Probably not the best. Any remarks or better solution?
它似乎工作。可能不是最好的。任何言论或更好的解决方案?
#1
0
So, I added a _ref_properties
field to the BaseModel
class. For the previous example, it would be _ref_properties = {'test2': 'test2_id'}
所以,我在BaseModel类中添加了一个_ref_properties字段。对于前面的示例,它将是_ref_properties = {'test2':'test2_id'}
Then I added this class method
然后我添加了这个类方法
@classmethod
def from_message(cls, message, *args):
attributes = {attr: getattr(message, attr) for attr in args}
for attribute, property in cls._ref_properties.items():
attributes[attribute] = db.Key(encoded=getattr(message, property))
entity = cls(**attributes)
return entity
And it seems to work. Probably not the best. Any remarks or better solution?
它似乎工作。可能不是最好的。任何言论或更好的解决方案?