Bring back _cls and _id fields just as they return in pymongo

This commit is contained in:
Eddie Linder 2018-12-20 00:39:32 +02:00
parent 2c6a744848
commit b04dc90cdf
2 changed files with 17 additions and 12 deletions

View File

@ -1845,10 +1845,7 @@ class BaseQuerySet(object):
# remove it from the doc (we always fetch it so that we can properly # remove it from the doc (we always fetch it so that we can properly
# construct documents). # construct documents).
fields = self._loaded_fields fields = self._loaded_fields
if fields and '_id' in doc and ( if fields and '_id' in doc and fields.value == QueryFieldList.EXCLUDE and '_id' in fields.fields:
(fields.value == QueryFieldList.ONLY and '_id' not in fields.fields) or
(fields.value == QueryFieldList.EXCLUDE and '_id' in fields.fields)
):
del doc['_id'] del doc['_id']
return doc return doc

View File

@ -4635,7 +4635,7 @@ class QuerySetTest(unittest.TestCase):
ip = StringField() ip = StringField()
class User(Document): class User(Document):
id = ObjectIdField('_id') id = StringField(primary_key=True)
name = StringField() name = StringField()
age = IntField() age = IntField()
price = DecimalField() price = DecimalField()
@ -4643,9 +4643,10 @@ class QuerySetTest(unittest.TestCase):
User.drop_collection() User.drop_collection()
User.objects.create(name="Bob Dole", age=89, price=Decimal('1.11')) User.objects.create(id='Bob', name="Bob Dole", age=89, price=Decimal('1.11'))
User.objects.create( User.objects.create(
name="Barack Obama", id='Barak',
name="Barak Obama",
age=51, age=51,
price=Decimal('2.22'), price=Decimal('2.22'),
last_login=LastLogin( last_login=LastLogin(
@ -4673,7 +4674,7 @@ class QuerySetTest(unittest.TestCase):
self.assertIsInstance(results[1], dict) self.assertIsInstance(results[1], dict)
self.assertEqual(results[0]['name'], 'Bob Dole') self.assertEqual(results[0]['name'], 'Bob Dole')
self.assertEqual(results[0]['price'], 1.11) self.assertEqual(results[0]['price'], 1.11)
self.assertEqual(results[1]['name'], 'Barack Obama') self.assertEqual(results[1]['name'], 'Barak Obama')
self.assertEqual(results[1]['price'], 2.22) self.assertEqual(results[1]['price'], 2.22)
users = User.objects.only('name', 'last_login').as_pymongo() users = User.objects.only('name', 'last_login').as_pymongo()
@ -4681,10 +4682,12 @@ class QuerySetTest(unittest.TestCase):
self.assertIsInstance(results[0], dict) self.assertIsInstance(results[0], dict)
self.assertIsInstance(results[1], dict) self.assertIsInstance(results[1], dict)
self.assertEqual(results[0], { self.assertEqual(results[0], {
'_id': 'Bob',
'name': 'Bob Dole' 'name': 'Bob Dole'
}) })
self.assertEqual(results[1], { self.assertEqual(results[1], {
'name': 'Barack Obama', '_id': 'Barak',
'name': 'Barak Obama',
'last_login': { 'last_login': {
'location': 'White House', 'location': 'White House',
'ip': '104.107.108.116' 'ip': '104.107.108.116'
@ -4701,7 +4704,7 @@ class QuerySetTest(unittest.TestCase):
db_field='password_salt', required=True) db_field='password_salt', required=True)
User.drop_collection() User.drop_collection()
User(email="ross@example.com", password_salt="SomeSalt", user = User(email="ross@example.com", password_salt="SomeSalt",
password_hash="SomeHash").save() password_hash="SomeHash").save()
serialized_user = User.objects.exclude( serialized_user = User.objects.exclude(
@ -4714,11 +4717,16 @@ class QuerySetTest(unittest.TestCase):
serialized_user = User.objects.exclude( serialized_user = User.objects.exclude(
'password_salt').only('email').as_pymongo()[0] 'password_salt').only('email').as_pymongo()[0]
self.assertEqual(set(['_id', 'email']), set(serialized_user.keys()))
serialized_user = User.objects.exclude(
'password_salt', 'id').only('email').as_pymongo()[0]
self.assertEqual(set(['email']), set(serialized_user.keys())) self.assertEqual(set(['email']), set(serialized_user.keys()))
serialized_user = User.objects.exclude( serialized_user = User.objects.exclude(
'password_salt').only('email').to_json() 'password_salt').only('email').to_json()
self.assertEqual('[{"email": "ross@example.com"}]', serialized_user) self.assertEqual('[{"_id": {"$oid": "%s"}, "email": "ross@example.com"}]' % user.id,
serialized_user)
def test_only_after_count(self): def test_only_after_count(self):
"""Test that only() works after count()""" """Test that only() works after count()"""