Code Monkey home page Code Monkey logo

django-mongodb's Introduction

MongoDB backend for Django

This backend is in the pre-alpha stage of development. Backwards incompatible changes may be made without notice. We welcome your feedback as we continue to explore and build.

Install and usage

Use the version of django-mongodb that corresponds to your version of Django. For example, to get the latest compatible release for Django 5.0.x:

pip install django-mongodb==5.0.*

The minor release number of Django doesn't correspond to the minor release number of django-mongodb. Use the latest minor release of each.

While django-mongodb only has pre-releases (alphas or betas), you'll see an error with a list of the available versions. In that case, include --pre to allow pip to install the latest pre-release.

For example, if django-mongodb 5.0 alpha 1 is the latest available version of the 5.0 release series:

$ pip install django-mongodb==5.0.*
ERROR: Could not find a version that satisfies the requirement
django-mongodb==5.0.* (from versions: ..., 5.0a1)

$ pip install --pre django-mongodb==5.0.*
...
Successfully installed ... django-mongodb-5.0a1 ...

Configure the Django DATABASES setting similar to this:

DATABASES = {
    "default": {
        "ENGINE": "django_mongodb",
        "NAME": "my_database",
        "USER": "my_user",
        "PASSWORD": "my_password",
    },
}

Known issues and limitations

  • The following QuerySet methods aren't supported:

    • annotate()
    • aggregate()
    • dates()
    • datetimes()
    • distinct()
    • extra()
    • select_related()
  • Queries with joins aren't supported.

  • DateTimeField doesn't support microsecond precision.

Troubleshooting

TODO

Credits

This project began by borrowing code from Django non-rel's MongoDB Engine, abandoned since 2015 and Django 1.6 (2-clause BSD license).

django-mongodb's People

Contributors

timgraham avatar blink1073 avatar wavev avatar jibola avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar Kelvin Adigwu avatar  avatar  avatar

django-mongodb's Issues

add JSONField support

Remove DatabaseFeatures.supports_json_field = False and work on fixing as much of tests/model_fields/test_jsonfield.py as possible.

Add a "not supported error" (instead of crashing) for QuerySet.aggregate()

For example:

./tests/runtests.py from_db_value.tests.FromDBValueTest.test_aggregation
Testing against Django installed in '/home/tim/code/django/django' with up to 3 processes
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_aggregation (from_db_value.tests.FromDBValueTest.test_aggregation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/from_db_value/tests.py", line 26, in test_aggregation
    maximum = CashModel.objects.aggregate(m=Max("cash"))["m"]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/query.py", line 604, in aggregate
    return self.query.chain().get_aggregation(self.db, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/query.py", line 620, in get_aggregation
    result = next(compiler.apply_converters((result,), converters))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 1500, in apply_converters
    value = converter(value, expression, connection)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django-mongodb/django_mongodb/operations.py", line 38, in convert_decimalfield_value
    value = decimal.Decimal(value)
            ^^^^^^^^^^^^^^^^^^^^^^
TypeError: conversion from dict to Decimal is not supported

It's probably complicated (impossible?) to implement full aggregate support, but for now it should not crash.

NotSupportedError at /admin/ QuerySet.select_related() is not supported on MongoDB.

Steps to reproduce

Successfully created a superuser. Once I created a superuser I attempted to work through the admin page. This then failed with the error QuerySet.select_related() is not supported.

<pre class="exception_value" style="padding: 0px; margin: 10px 0px; font-size: 1.5em; white-space: pre-wrap; word-break: break-word; font-family: sans-serif; color: rgb(87, 87, 87); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">QuerySet.select_related() is not supported on MongoDB.</pre>
Request Method: | GET
-- | --
http://127.0.0.1:8000/admin/
5.0.6
NotSupportedError
QuerySet.select_related() is not supported on MongoDB.
/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py, line 113, in check_query
django.contrib.admin.sites.index
/opt/homebrew/Caskroom/miniforge/base/envs/django/bin/python
3.12.3
['/Users/jib/mongo/starterapp/storefront',  '/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python312.zip',  '/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12',  '/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/lib-dynload',  '/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages']

Environment

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 5.0.6
Python Version: 3.12.3
Installed Applications:
['apps.MongoAdminConfig',
 'apps.MongoAuthConfig',
 'apps.MongoContentTypesConfig',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'playground']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

QuerySet.update() only updates one document if the field is unique

See comment.

A failing test (target is a field with unique=True):

    def test_multiple_unique(self):
        a_foo = Foo.objects.create(target="aaa")
        b_foo = Foo.objects.create(target="bbb")
        Foo.objects.update(target="ccc")
        a_foo.refresh_from_db()
        b_foo.refresh_from_db()
        self.assertEqual(a_foo.target, "ccc")
        self.assertEqual(b_foo.target, "ccc")

======================================================================
FAIL: test_multiple_unique (update.tests.AdvancedTests.test_multiple_unique)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/update/tests.py", line 153, in test_multiple_unique
    self.assertEqual(b_foo.target, "ccc")
AssertionError: 'bbb' != 'ccc'
- bbb
+ ccc

NotSupportedError: Ordering can't span tables on MongoDB (content_type__app_label)

Steps to reproduce

Followed instructions for initial setup from #18. Created my first admin account. From there, I called python manage.py makemigrations which worked. Afterward I then called python manage.py migrate which led to this initial error.

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying admin.0004_alter_logentry_id... OK
  Applying auth.0013_alter_group_id_alter_permission_id_alter_user_id... OK
  Applying contenttypes.0003_alter_contenttype_id... OK
Traceback (most recent call last):
  File "/Users/jib/mongo/starterapp/storefront/manage.py", line 22, in <module>
    main()
  File "/Users/jib/mongo/starterapp/storefront/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 383, in handle
    emit_post_migrate_signal(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/sql.py", line 52, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/contrib/auth/management/__init__.py", line 91, in create_permissions
    all_perms = set(
                ^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/db/models/query.py", line 400, in __iter__
    self._fetch_all()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/db/models/query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 47, in results_iter
    results = self.build_query(columns).fetch()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 140, in build_query
    query.order_by(self._get_ordering())
                   ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 170, in _get_ordering
    raise NotSupportedError("Ordering can't span tables on MongoDB (%s)." % order)
django.db.utils.NotSupportedError: Ordering can't span tables on MongoDB (content_type__app_label).

mongod crashes while running the Django test suite

For example, mongod terminates with "Aborted (core dumped)" in the middle of $ ./runtests.py sessions_tests or $ ./runtests.py lookup. It may be a problem specific to my environment (Ubuntu 20.04) as it has not happened while running the tests on GitHub actions.

$ mongod --version
db version v7.0.8
Build Info: {
    "version": "7.0.8",
    "gitVersion": "c5d33e55ba38d98e2f48765ec4e55338d67a4a64",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

implement dbshell

django_mongodb/client.py is stubbed out, but DatabaseClient.settings_to_cmd_args_env() needs to be implemented to construct the connection arguments for the mongo shell.

refactor MongoQuery.add_filters()

Jib: We should cut a task to break up and simplify this function. It's far too lengthy and confusing, especially as we get into the negation logic. It isn't encapsulated well, so it gets hard to remember what was happening previously.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.