Pymongo request is normal, but Mongoengine is not asking for anything
Very strange: Pymongo's request is normal, but Mongoengine doesn't ask for anything:
class VkWallPostListView(ListView):
model = VkWallPost
context_object_name = "vk_list"
def get_template_names(self):
return ["blog/vk_list.html"]
def get_queryset(self):
wallposts = VkWallPost.objects
if 'all_posts' not in self.request.GET:
#wallposts = wallposts.filter(text='S')
wallposts = VkWallPost._get_collection().find({"text":'S'})
tag = self.request.GET.get('tag', None)
if tag:
wallposts = wallposts.filter(tags=tag)
return wallposts
The option wallposts = VkWallPost._get_collection().find({"text":'S'})
returns objects, but the same Mongoengine wallposts = wallposts.filter(text='S')
does not work - empty results, no errors! Moreover, I have the same class as the query to another collection - Mongoengine works fine there.
source to share
It looks like your VkWallPostListView doesn't inherit directly from mongoengine.Document
. I recently ran into this where an identical database schema model returned nothing when requested, when inherited from something other than the base class Document
. It turns out that mongoengine adds a hidden field to the documents of the child classes called _cls
and automatically checks this when requested.
For example:
If I have a TextHolder class which is the base of my Post class
class TextHolder(mongoengine.Document):
text = mongoengine.StringField()
meta = { 'allow_inheritance' : True,
'abstract' : True }
class Post(TextHolder):
user = mongoengine.ReferenceField(User)
If I tried to query posts that already existed in the database, they won't have the fields _cls
defined in their documents. So the way to request documents that do not conform to this standard is to do this.
Post.objects(user=user_to_query_by, class_check=False)
This will give me all the objects ignoring the field _cls
, not a check against the current model class name.
source to share