简体   繁体   中英

save() doesn't work in Mongoengine

I'm trying to perform a simple insert operation using Mongoengine and Django.

Regarding my project structure simply I have a project, AProject and an app, AnApp. I have a running mongo in a remote machine with an IP of XXXX I am able to insert document using Robomongo in it.

I have removed the default Database configuration part of the settings.py located inside the AProject directory. The newly added lines are shown below:

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

import mongoengine

# ----------- MongoDB stuff

from mongoengine import register_connection
register_connection(alias='default', name='AProject', host='X.X.X.X')

# ----------

Now let me show the models.py and the views.py located inside AnApp.

models.py

from mongoengine import Document, StringField

class Confession(Document):
    confession = StringField(required=True)

views.py

from django.http import HttpResponse

from models import Confession
from mongoengine import connect

def index(request):
    connect('HacettepeItiraf', alias='default')
    confession = Confession()
    confession.confession = 'First confession from the API'
    print(confession.confession + ' Printable') # The output is --First confession from the API Printable--
    print(confession.save()) # The output is --Confession object--
    return HttpResponse(request)

The urls.py located inside AProject is simply as below:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^confessions/', include('confession.urls')),
    url(r'^admin/', admin.site.urls),
]

When I enter http://127.0.0.1:10000/confessions/ I see a blank screen which I expect. However there is nothing saved from the API. I get the expected output except Confession object .

How can I solve this problem?

EDIT: I found the concrete proof that currently MongoEngine 's support for Django is unstable and it is corresponding to Django version 1.9:

  1. MongoEngine documentation on Django support states:

    Django support has been split from the main MongoEngine repository. The legacy Django extension may be found bundled with the 0.9 release of MongoEngine.

    and

    6.1. Help Wanted!

    The MongoEngine team is looking for help contributing and maintaining a new Django extension for MongoEngine! If you have Django experience and would like to help contribute to the project, please get in touch on the mailing list or by simply contributing on GitHub .

  2. Which leads to this repo , where the following is stated:

    THIS IS UNSTABLE PROJECT, IF YOU WANT TO USE IT - FIX WHAT YOU NEED
    Right now we're targeting to get things working on Django 1.9

So it may be possible that it cannot play well with Django at this state or with your version and thus the problem occurs.


Initial attempt, leaving it here for legacy reasons.

I believe that the problem occurs on how you are initializing your object, although I do not have a set up to test this theory.

It is generally considered that is better to make a new object with the .create() method:

 def index(request): connect('HacettepeItiraf', alias='default') confession = Confession.objects.create( confession='First confession from the API' ) print(confession.confession + ' Printable') confession.save() return HttpResponse(request) 

Have a look at the Django & MongoDB tutorial for more details.
In this tutorial, the supported Django version is not mentioned, but I haven't found concrete proof that MongoDB Engine can or can't play well with Django version > 1.8.

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM