Skip to content

Instantly share code, notes, and snippets.

@hmarr
Created December 3, 2010 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hmarr/727195 to your computer and use it in GitHub Desktop.
Save hmarr/727195 to your computer and use it in GitHub Desktop.
from mongoengine import *
from mongoengine.queryset import QuerySet
class PostQuerySet(QuerySet):
def delete(self, safe=False):
ids = [post.id for post in self]
Comment.objects(post__in=ids).delete(safe=safe)
super(PostQuerySet, self).delete(safe=safe)
class Post(Document):
title = StringField()
meta = {'queryset_class': PostQuerySet}
def delete(self, safe=False):
Comment.objects(post=self.id).delete(safe=safe)
super(Post, self).delete(safe=safe)
class Comment(Document):
post = ReferenceField(Post)
@nvie
Copy link

nvie commented Dec 3, 2010

I think that this might just work, but the implementation is very ad hoc. I'd have to know upfront which objects refer to the Post document, which is not really useful when building extensible applications. I've forked this Gist and I'm preparing a patch that implements the idea (at least in pseudo code for now) that solves this problem in a general sense. Hang on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment