views:

584

answers:

3

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.

+1  A: 

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.

A: 

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.

Nick Johnson
A: 

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):

obj1 = db.get(obj2.reference)

if not obj1: # Referenced entity was deleted.

Mehmet