删除 AppEngine 中错误的 ReferenceProperty 属性
大多数时候,从模型属性中得到的错误会在保存数据时发生。 例如,如果您尝试将字符串保存为 IntegerProperty,则会导致错误。
唯一的例外(无双关语)是 ReferenceProperty。 如果您有大量引用,并且您没有完全小心留下错误引用,那么通常会遇到类似“TemplateSyntaxError:渲染时捕获异常:ReferenceProperty 无法解析”之类的错误。
这是如果视图中只有一个错误引用的情况。 噢。
我可以编写一个 try/ except 块来尝试访问所有引用属性并在引发异常时删除它们,但是如果有比我所用的方法更通用的方法,那么此功能肯定对许多其他开发人员有用有写作能力。 我想这将需要一个模型类型列表,并尝试访问每个模型中每个实体的每个引用属性,如果引发异常,则将该属性设置为 None 。
我会看看我自己是否可以做到这一点,但是提供一些建议/片段来帮助我开始肯定会有所帮助。
Most of the time, the errors you get from your model properties will happen when you're saving data. For instance, if you try saving a string as an IntegerProperty, that will result in an error.
The one exception (no pun intended) is ReferenceProperty. If you have lots of references and you're not completely careful about leaving in bad references, it's common to be greeted with an error like "TemplateSyntaxError: Caught an exception while rendering: ReferenceProperty failed to be resolved".
And this is if there's only one bad reference in the view. D'oh.
I could write a try/except block to try to access all the reference properties and delete them if an exception is raised, but this functionality could surely be useful to many other developers if there was a more generic method than the one I'd be capable of writing. I imagine it would take a list of model types and try to access each reference property of each entity in each model, setting the property to None if an exception is raised.
I'll see if I can do this myself, but it would definitely help to have some suggestions/snippets to get me started.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的项目也遇到类似的困难。 当我编写应用程序的 beta 版本时,我确实创建了很多死链接,之后要理清事情确实很痛苦。 理想情况下,该工具还必须报告有问题的引用,以便您可以查明代码中的问题。
I'm having similar difficulties for my project. As I code the beta version of my application, I do create a lot of dead link and its trully a pain to untangle things afterward. Ideally, this tool would have to also report of the offending reference so that you could pin-point problems in the code.
您可以扩展和自定义 ReferenceProperty 以不引发此异常,但随后它需要返回一些内容(可能是 None),在这种情况下,您的模板在尝试访问返回对象的属性时将简单地引发异常。
更好的方法是在渲染模板之前获取引用属性并检查其是否有效。 ReferenceProperties 缓存其引用,因此预取不会导致额外的数据存储调用。
You could extend and customize ReferenceProperty to not throw this exception, but then it'll need to return something - presumably None - in which case your template will simply throw an exception when it attempts to access properties on the returned object.
A better approach is to fetch the referenceproperty and check it's valid before rendering the template. ReferenceProperties cache their references, so prefetching won't result in extra datastore calls.
该异常实际上是一个等待修复一段时间的错误(请参阅 http://code.google.com/p/googleappengine/issues/detail?id=426)。 理想情况下,您应该能够像这样测试引用是否有效(来自应用程序引擎文档):
That exception is actually a bug that's been waiting to be fixed for a while (see http://code.google.com/p/googleappengine/issues/detail?id=426). Ideally you should be able to test whether the reference is valid like this(from app engine documentation):