4👍
this is answered here. with mongoengine 0.10 we can see that /usr/lib/python2.7/site-packages/mongoengine/ will not have django package in it. Install mongoengine 0.9 using sudo pip install mongoengine==0.9 and the django package (support or extension) will be available.
1👍
I want to use a mongodb database together with the django framework
If this is your goal there are several ways to accomplish it. Depending on the strategy you take, do factor in the trade offs.
Use a MongoDB compatible model framework (MongoEngine): You can completely avoid using the Django models in your project and use MongoEngine or Ming in you django projects. However will miss out on: 1500+ core contributors to the project, hourly fixes and ticket resolution, ramp down on the expertise of existing Django models and ramp up on the new model framework. Miss out on Django’s contrib models like Admin, Sessions, Users, Auth, etc., contrib modules for your project.
Django SQL to MongoDB transpiler — Djongo: Translate Django SQL query syntax generated by the Django ORM into pymongo commands. Djongo is one such SQL to MongoDB query compiler. It translates every SQL query string into a mongoDB query document. As a result, all Django models and related modules work as is.
0👍
There is some issue with using the latest version of pymongo
with Django.
Try downgrading pymongo
to 2.8 and it should work.
- [Django]-Django filter objects based on property in ForeignKey set
- [Django]-How to get something to appear on every page in Django?
- [Django]-How can I install plugin into CkEditor, Django
- [Django]-Which Server for Django
0👍
Define envs in .env file for database connection:
DB_NAME=mock
DB_USER=test
DB_PASS=secret
HOST=localhost
In app/settings.py:
import mongoengine
# Load envs
import environ
env = environ.Env()
env.read_env(env.str('ENV_PATH', '../.env'))
mongoengine.connect(db=env('DB_NAME'), host=env('HOST'), username=env('DB_USER'), password=env('DB_PASS'),serverSelectionTimeoutMS=1000)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
},
}
TEST_RUNNER = 'app.tests.NoSQLTestRunner'
In app/tests:
class NoSQLTestRunner(DiscoverRunner):
def __init__(self, mocking, **kwargs):
super().__init__(**kwargs)
def setup_databases(self, **kwargs):
pass
def teardown_databases(self, old_config, **kwargs):
pass
def setup_test_environment(self, **kwargs):
super(NoSQLTestRunner, self).setup_test_environment(**kwargs)
class NoSQLTestCase(TestCase):
def __init__(self, *args, **kwargs):
super(NoSQLTestCase, self).__init__(*args, **kwargs)
@classmethod
def setUpClass(cls):
disconnect()
connect('mongoenginetest', host='mongomock://localhost')
@classmethod
def tearDownClass(cls):
disconnect()
def _fixture_setup(self):
pass
def _fixture_teardown(self):
pass
Now you can create models as represented in mongoengine docs, and write tests:
from app.tests import NoSQLTestCase
from myapp.models import MyModel
class CustodianOrganizationTest(NoSQLTestCase):
def test_my_testcase(self)
instance = MyModel()
instance.save()
assumed = "Foo"
self.assertEqual(instance.foo, assumed)
...
and run tests by python3 manage.py test
This setup is tested running succesfully with:
python 3.9
Django 3.1.3
mongoengine 0.22.0
pymongo 3.11.2
- [Django]-How to install python3 and Django using userdata in AWS EC2 with aws cli boto3
- [Django]-Reverse for 'plan_edit' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['palan/edit$']
- [Django]-Return pdf response from stdout with Django