views:

71

answers:

3

Hi all,

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 the 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.

A: 

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.

Ilian Iliev
In my last project I had created some pre_delete signals which would set the reference property to a default user once a user is deleted. But here this method does not seem to be a good way due to a large set of models.
anand
BTW Ilian, how did you manage to avoid the issue using exception handling on both front end and admin panel? I will appreciate some example.
anand
Unfortunately I didn`t find a way to handle exceptions thrown from the templates, only the those from the python files. Perhaps you can use something like custom template tag to check whether the reference exist and to do something if not but I am not sure.
Ilian Iliev
A: 

Two possible solutions:

  • Check if the reference still exists, before you access it:

    if not obj.reference:
    # Referenced entity was deleted

  • When you delete a model object that may be referenced, query all models that might reference it, and set their reference property to None.

geofrank
When you delete a model object that may be referenced, query all models that might reference it, and set their reference property to None. I think this is what actually happens automatically when a reference is deleted, it becomes None ?
anand
No such thing happens automatically. All references will stay (and be invalid). http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html#References
geofrank
+1  A: 

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:

  1. 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.
  2. "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.

Cameron
Thanks for your reply cameron, but the solution is not feasible at this stage of development with a large set of models.
anand