Removing erroneous ReferenceProperty in AppEngine

In most cases, the errors you get from model properties will run when the data is saved. For example, if you try to store a string as an IntegerProperty, it will result in an error.

The only exception (no pun intended) is ReferenceProperty. If you have a lot of links and are not very careful about leaving them in bad links, you usually encounter an error like "TemplateSyntaxError: Render Exception Thrown: ReferenceProperty could not be resolved".

And that's if there is only one bad link in the view. D'o.

I could write a try / except block to try and access all the properties of the link and remove them if an exception is thrown, but this function could certainly be useful to many other developers if there was a more general method than the one I am " I will be able to write, I guess this would require a list of model types and try to access every reference property of every object in every model by setting the property to None if an exception is thrown.

I'll see if I can do it myself, but it will definitely help to have some suggestions / snippets to get me started.

0


source to share


3 answers


I am having similar difficulties for my project. When I code the beta of my app I create a lot of dead link and its a trully pain to unravel things after that. Ideally, this tool should also report link breaking so you can run into problems in your code.



+1


source


You can extend and customize the ReferenceProperty to not throw this exception, but then it will need to return something - presumably None - in which case your template will just throw an exception when it tries to access the properties of the returned object.



The best approach is to get the referenced property and validate it before rendering the template. ReferenceProperties caches its references, so prefetching will not result in additional calls to the data store.

0


source


This exception is actually a bug awaiting some time to fix (see http://code.google.com/p/googleappengine/issues/detail?id=426 ). Ideally, you should check if the link is actually valid (from the application engine documentation):

obj1 = db.get(obj2.reference)

if not obj1:
  # Referenced entity was deleted.

      

0


source







All Articles