简体   繁体   中英

Fixtures for Mongoengine with Django not working

I am using Mongoengine(version: 0.9.0 ) with Django(version: 1.8). This is my settings.py

DATABASES = {
 'default': {
   'ENGINE': 'django.db.backends.dummy'
    }
  }  
MONGO_DBNAME = "mydatabasename"
MONGO_HOSTNAME = "localhost"

connect(MONGO_DBNAME, host=MONGO_HOSTNAME)

I want to have fixtures for the application. I have created initial_data.json in myapp/fixtures/ location.

When I run the command python manage.py dumpdata , I get the following error :

CommandError: Unable to serialize database: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Questions:

1) Any workaround for this problem ?

2) Is there any other way to load the initial data ?

References at this link

Thank you

Mongoengine itsn a backend(in django terminology). Its has own models (schemas) and DOM (like ORM in docuemnt db's) but it dont have a Django backend adapters.

You can use it. But there is issue while workind with out-of-box Django solution like Tests, Fixtures, etc. You need to write your own loader, sadenly but true.

I see 2 options here:

Ill write my own fixture loader for tests. I have a json file where mapped all fixture file ill need to load to db. So a fast example here:

import bson
import os
from django.conf import settings
from mongoengine.connection import get_db

def _get_db(self):
    self.db = get_db()

def _load_fixtures(self, clear_before_load=True):
    """
    Load to db a fixtures from folder fixtures/{{DB_NAME}}/{{COLLECTION_NAME}} before each test.
    In file fixtures.json mapped collection name and file name for it.
    """
    fixture_path = lambda file_name: os.path.join(settings.FIXTURES_DIR, self.db.name, file_name)
    with open(settings.COLLECTION_FIXTURES_PATH) as file_object:
        db_collections = loads(file_object.read())
        for collection_name, filename in db_collections.items():
            collection = self.db[collection_name]
            if clear_before_load:
                collection.remove()
            path = fixture_path(filename)
            if os.path.exists(path) and os.path.isfile(path):
                with open(path, 'r') as raw_data:
                    collection_data = bson.decode_all(raw_data.read())
                    for document in collection_data:
                        collection.save(document)

There is no support for fixtures on mongoengine , and I don't think the mongoengine team is continuing the plugin as of version 0.9.0.

What I ended up doing to load initial data for mongoDB is to create a script called startup.py in my project folder.

startup.py:

from {{app}}.models import Sample

def init():
    if Sample.objects(name="test").count() == 0: # a flag to prevent initial data repetition
        Sample(name="test").save()

Next is to run this script on Django's startup. The entry point of Django project is when DJANGO_SETTINGS_MODULE is first loaded at wsgi.py :

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{project_name}}.settings")

import {{project_name}}.startup as startup
startup.init()

application = get_wsgi_application()

With this setup, when you run python manage.py runserver , the init() on startup.py will run and the data you set will be inserted to the DB.

Hope this helps.

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