Django Google app engine reference issues
I am working on an application on Django and google application engine. In my application I have several models with several ReferenceProperty fields. The issue is that if any of the ReferenceProperty field gets deleted it produces a ReferenceProperty related errors in all the other models where it has been used. What I want is, when a field is deleted say a User is deleted, all the fields having User as t开发者_JS百科he ReferenceProperty should still work without any error messages displaying the associated user as unavailable or something like that.
Can someone please suggest how that can be done?
Thanks in advance.
You could also just set a flag, say deleted
, on the entity you're deleting, and then leave it in the datastore. This has the advantage of avoiding all referential integrity problems in the first place, but it comes at the cost of two main disadvantages:
- All your existing queries need to be changed to deal with entities that have the
deleted
property set, either by omitting them from the result set or by special casing them somehow. - "Deleted" data stays in the datastore; this can bloat the datastore, and also may not be an option for sensitive information.
This doesn't really solve your problem at all, but I thought I'd mention it for completeness's sake.
Two possible solutions:
Check if the reference still exists, before you access it:
if not obj.reference:
# Referenced entity was deletedWhen you delete a model object that may be referenced, query all models that might reference it, and set their reference property to None.
When I have the same problem ago, I could not find a general solution. The only way I found is to do try/except for every reference property. If you find another answer post it here.
精彩评论