Forums

Model.objects return empty QuerySet on Django 1.7 and MongoEngine 0.9

I am trying to connect to MongoLab with MongoEngine for my Django project. The authentication works, the Model is defined and I do not get any error or warning. However Mymodel.objects return an empty QuerySet in the View. Is there any known issue about that? I have tried many settings, however the result is the same.

I would be grateful if anybody could point me to a solution or where to look.

Ali

What happens if you try manually creating and querying objects in a console from python manage.py shell?

Thanks Harry! When I run “python manage.py shell”, I get the following warning: /home/<username>/.virtualenvs/dj17/lib/python3.4/sitepackages/mongoengine/django/mongo_auth/models.py:33: RemovedInDjango18Warning: ‘MongoUserManager.get_query_set’ method should be renamed ‘get_queryset’. class MongoUserManager(UserManager):

<console starts>

The warning is about Django 1.8, right?

I imported the model and did a query like: from kategorize.models import Haber

for h in Haber.objects: print(h) --> since Haber.objects is empty, there is not any result for print.

len(Haber.objects) is 0.

Ali

Shouldn't the query be:

Haber.objects.all()

?

The mongoengine user guide suggests that Haber.objects would return a valid set to iterate over: http://docs.mongoengine.org/en/latest/guide/querying.html

I run: Haber.objects.all()

I get: [] --> empty list

OK. Now what happens if you try and create some records, and then re-run the query?

The database contains a collection named: haberci, which contains documents like:

{
    "_id": {
        "$oid": "54eb8cb386ddb6593bc45f9f"
    },
    "atanan_kategori": "ekonomi",
    "baslik": "Dondurucu soğukta yemek iddiası"
}

My Model is: from django.db import models from mongoengine import Document, StringField

class Haber(Document):
    meta = {'collection' : 'haberci'}
    baslik = StringField()
    atanan_kategori = StringField()

    def baslikstr(self):
            return self.baslik

    def atanan_kategoristr(self):
            return self.atanan_kategori

Should it work like that?

I don't know. I've never used Mongodb. Isn't there a way of creating objects via the django ORM? Like

haber = Haber()
haber.baslik = 'foo'
haber.atanan_kategori = 'bar'
haber.save()
print Haber.objects.all()

Yes!! It works like that. My database on Mongolab has two collections named 'haberci', with the same name, now. The question turned to be: why Django does not use the available collection?

That does sound annoying... Maybe it's worth a look thru the mongoengine docs, or a question to their mailing list?

Thanks Harry, with your help, I have some direction to investigate the issue further.

Ali

I asked MongoLab support. The only reason we could imagine that the document structure in the database and my model's structure does not match, but the fields that I want to map match. I would expect a bit more flexibility from a NOSQL database, or from a Web development environment. Anyway, I will continue by inserting the documents from the Model and read them from the Model.

Ali

Now that you have two collections with the same name, is it worth adding things to the second collection (the one connected to django), but doing so via the mongo server directly, and seeing if they appear in django queries?

Yes, it works. But I continue by adding documents via the model. Thanks for your time Harry!