Skip to content

Add write concern support #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

scarlet2131
Copy link

@scarlet2131 scarlet2131 commented Jun 26, 2025

This PR implements write concern support to replace the broken implementation that didn't work with latest PyMongo APIs.
Changes:

  • Add WriteConcern import and parsing in DatabaseWrapper

  • Support dict, string, and int configuration formats

  • Apply write concern automatically to all collections

  • Add comprehensive tests

Usage:

DATABASES = { 'default': { 'ENGINE': 'django_mongodb_backend', 'NAME': 'mydb', 'OPTIONS': { 'WRITE_CONCERN': {'w': 'majority', 'j': True} # or 'majority' or 2 } } }
fixes #6

- Add WriteConcern import from pymongo - Add write concern parsing in DatabaseWrapper.__init__() - Support dict, string, and int write concern formats - Apply write concern to collections in get_collection() - Add comprehensive tests for write concern functionality
@timgraham
Copy link
Collaborator

Thanks for your interest in contributing, Monisha.

Since MongoClient takes write concern kwargs (see below "Write Concern options" in the MongoClient API doc, I wonder if this is enabling any behavior that's not already possible using that. Anything in DATABASES' OPTIONS is already passed to MongoClient. And with that in mind, doesn't putting 'WRITE_CONCERN' in OPTIONS crash MongoClient?

 File "/home/tim/code/django/django/db/backends/base/base.py", line 256, in connect self.connection = self.get_new_connection(conn_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/code/django/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/code/django-mongodb/django_mongodb_backend/base.py", line 220, in get_new_connection conn = MongoClient(**conn_params, driver=self._driver_info()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/synchronous/mongo_client.py", line 834, in __init__ opts = self._validate_kwargs_and_update_opts(keyword_opts, opts) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/synchronous/mongo_client.py", line 1003, in _validate_kwargs_and_update_opts dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items()) File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/synchronous/mongo_client.py", line 1003, in <genexpr> dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/common.py", line 843, in validate validator = _get_validator(option, VALIDATORS, normed_key=option.lower()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/common.py", line 838, in _get_validator raise_config_error(key, suggestions) File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/common.py", line 174, in raise_config_error raise ConfigurationError(msg) pymongo.errors.ConfigurationError: Unknown option: WRITE_CONCERN. Did you mean one of (readconcernlevel, datetime_conversion, read_preference) or maybe a camelCase version of one? Refer to docstring. 
@timgraham timgraham changed the title Add write concern support for issue #6 Add write concern support Jun 26, 2025
@scarlet2131
Copy link
Author

scarlet2131 commented Jun 28, 2025

Hey @timgraham, thanks for looking at this! I think there might be some confusion about what my code actually does.

The crash you mentioned won't happen because I'm not passing WRITE_CONCERN to MongoClient. My code pulls it out of OPTIONS first and handles it separately:

# This extracts WRITE_CONCERN before MongoClient sees it self._write_concern = self._parse_write_concern(settings_dict.get('OPTIONS', {}).get('WRITE_CONCERN'))

So the difference is:

  • Your way: Put w, j, etc. directly in OPTIONS → MongoClient handles it (client-level)
  • My way: Put them under WRITE_CONCERN → I handle it per-collection

I went with collection-level because I thought it gave more control, but honestly I'm not sure which approach you'd prefer for this project. Would client-level write concern work better? I can easily change it if that's what makes more sense.

Just let me know what direction you want to go - happy to adjust!

@timgraham
Copy link
Collaborator

The line you quoted doesn't remove WRITE_CONCERN from OPTIONS, so it does crash as far as I tested:

 File "/home/tim/code/django-mongodb/django_mongodb_backend/base.py", line 198, in get_new_connection conn = MongoClient(**conn_params, driver=self._driver_info()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... File "/home/tim/.virtualenvs/django312/lib/python3.12/site-packages/pymongo/common.py", line 174, in raise_config_error raise ConfigurationError(msg) pymongo.errors.ConfigurationError: Unknown option: WRITE_CONCERN. Did you mean one of (readconcernlevel, datetime_conversion, read_preference) or maybe a camelCase version of one? Refer to docstring. 

How does handling write concern at the collection level give more control (if you pass the same options to all collections)?

Are you a MongoDB user who is using this project and needs this functionality?

As Jib said, I believe we don't need any changes to support write concern after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants