Skip to content

save issue fixed #223

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: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions django_mongodb_engine/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def insert(self, docs, return_id=False):
doc['_id'] = doc.pop(self.query.get_meta().pk.column)
except KeyError:
pass
if doc.get('_id', NOT_PROVIDED) is None:
if doc.get('_id',NOT_PROVIDED) is None:
if len(doc) == 1:
# insert with empty model
doc.clear()
Expand All @@ -386,11 +386,25 @@ def insert(self, docs, return_id=False):

collection = self.get_collection()
options = self.connection.operation_flags.get('save', {})

# Solution to fix save is deprecated.
# if id is in the doc, the data needs to be updated else data needs to be inserted
try:
doc['_id']
is_update = True
update_spec = {'$set':doc}
except KeyError:
is_update = False
if return_id:
return collection.save(doc, **options)
if is_update:
return collection.update_one({'_id':doc['_id']},update_spec,True)
else:
return collection.insert_one(doc)
else:
collection.save(doc, **options)
if is_update:
#return collection.save(doc, **options)
collection.update_one({'_id':doc['_id']},update_spec,True)
else:
collection.insert_one(doc)


# TODO: Define a common nonrel API for updates and add it to the nonrel
Expand All @@ -399,7 +413,6 @@ class SQLUpdateCompiler(NonrelUpdateCompiler, SQLCompiler):
query_class = MongoQuery

def update(self, values):
multi = True
spec = {}
for field, value in values:
if field.primary_key:
Expand All @@ -426,7 +439,7 @@ def update(self, values):
if field.unique:
multi = False

return self.execute_update(spec, multi)
return self.execute_update(spec)

@safe_call
def execute_update(self, update_spec, multi=True, **kwargs):
Expand All @@ -437,9 +450,8 @@ def execute_update(self, update_spec, multi=True, **kwargs):
return 0
options = self.connection.operation_flags.get('update', {})
options = dict(options, **kwargs)
info = collection.update(criteria, update_spec, multi=multi, **options)
if info is not None:
return info.get('n')
info = collection.update_many(criteria, update_spec, **options)
return info


class SQLDeleteCompiler(NonrelDeleteCompiler, SQLCompiler):
Expand Down