GAE:在 ModelChoiceProperty 框中使用 key() 的属性
我有一个模型 User
,它在另一个模型 Group
中显示为 ReferenceProperty
。
当我使用 Meta
为 Group
创建表单时,表单的值包含大量生成的字符串。我想停止此操作,并改用 User
的 username
字段。
我已经定义了一个key_name
。但是,str(user.key()) 仍然给出生成的字符串。我可以重写key()
,但这会很糟糕。有什么想法吗?我希望组表单使用用户名作为 ModelChoiceProperty 值,并且表单仍然可以验证和保存。根据消息来源,当前表单打印 key()
的字符串值。
I have a model User
which appears as a ReferenceProperty
in another model, Group
.
When I create a form for Group
, using Meta
, the form's values contain lots of generated strings. I'd like to stop this, and use the username
field of User
instead.
I already define a key_name
. However, str(user.key())
still gives a generated string. I could override key()
, but that would be bad. Any thoughts? I want the Group form to use username
s for the ModelChoiceProperty values, and the form to still validate and save. Currently the form prints the string value of key()
, according to the source.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
db.model
中的key()
是一个对象,其中包含一堆不同的信息,包括对象的类型、其名称
和一个id
。所以我想
key().name()
会返回你想要的东西?在文档中,它描述了所有这些。
The
key()
in adb.model
is an object that contains a bunch of different information, including the kind of object, itsname
and anid
.So I'm thinking that
key().name()
would return what you want?In the docs, it describes all of this.
经过仔细思考后,我认为正确的答案是“不要这样做”。实体仍将具有键,并且这些键将对应于生成的字符串。必须对表单进行修改才能使其正常工作,因此从本质上使代码看起来更漂亮看起来很麻烦。
Having thought about this a little bit harder, I think the correct answer is "don't do that". The
Entity
s will still have keys, and those keys will correspond to a generated string. The form would have to be hacked to make it work too, so this looks like a ton of hassle to essentially make the code a tiny bit prettier looking.