Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoEngine query to get Post's author and Comments authors for Posts (at the same time)
    text
    copied!<p>I'm working on a web project with <strong>Django and MongoDB</strong> as my database (using MongoEngine to connect them). I have to create Celery task to cleanup old user accounts. I need to only clean lazy users accounts without any content after one month (lazy users are automatically created users when they first connect to the website). What count as content? Any posts from the user or comments on any of the posts.</p> <p>I did it like this, but I want to transform this into query if possible:</p> <pre><code>def clean_inactive_lazy_users(): users_with_content = [] for post in api_models.Post.objects: users_with_content.append(post.author) for comment in post.comments: users_with_content.append(comment.author) users_with_content = list(set(users_with_content)) for user in account_models.User.objects: if not user.is_authenticated() and (timezone.now() - user.connection_last_unsubscribe).days &gt;= settings.DELETE_LAZY_USER_AFTER_DAYS and user not in users_with_content: user.delete() </code></pre> <p>The models look like this:</p> <p>base.py</p> <pre><code>class AuthoredEmbeddedDocument(mongoengine.EmbeddedDocument): author = mongoengine.ReferenceField(models.User, required=True) class AuthoredDocument(mongoengine.Document): author = mongoengine.ReferenceField(models.User, required=True) </code></pre> <p>api_models:</p> <pre><code>from . import base class Comment(base.AuthoredEmbeddedDocument): """ This class defines document type for comments on posts. """ class Post(base.AuthoredDocument): """ This class defines document type for posts. """ </code></pre> <p>account_models:</p> <pre><code>class User(auth.User): def is_authenticated(self): return self.has_usable_password() </code></pre> <p>Hopefully I provided enough information so you can help me with the problem. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload