Renamed Document._id to id (still _id in DB)
Although MongoDB uses _id, underscore prefixed attributes imply private access in Python and are sometimes may not be accessed (e.g. in the Django template language), but id should be public.
This commit is contained in:
		| @@ -41,7 +41,7 @@ class DocumentTest(unittest.TestCase): | ||||
|         self.assertEqual(Person._fields['name'], name_field) | ||||
|         self.assertEqual(Person._fields['age'], age_field) | ||||
|         self.assertFalse('non_field' in Person._fields) | ||||
|         self.assertTrue('_id' in Person._fields) | ||||
|         self.assertTrue('id' in Person._fields) | ||||
|         # Test iteration over fields | ||||
|         fields = list(Person()) | ||||
|         self.assertTrue('name' in fields and 'age' in fields) | ||||
| @@ -145,7 +145,7 @@ class DocumentTest(unittest.TestCase): | ||||
|         person['name'] = 'Another User' | ||||
|         self.assertEquals(person['name'], 'Another User') | ||||
|  | ||||
|         # Length = length(assigned fields + _id) | ||||
|         # Length = length(assigned fields + id) | ||||
|         self.assertEquals(len(person), 3) | ||||
|  | ||||
|         self.assertTrue('age' in person) | ||||
| @@ -160,7 +160,7 @@ class DocumentTest(unittest.TestCase): | ||||
|             content = StringField() | ||||
|          | ||||
|         self.assertTrue('content' in Comment._fields) | ||||
|         self.assertFalse('_id' in Comment._fields) | ||||
|         self.assertFalse('id' in Comment._fields) | ||||
|         self.assertFalse(hasattr(Comment, '_meta')) | ||||
|  | ||||
|     def test_save(self): | ||||
| @@ -174,14 +174,14 @@ class DocumentTest(unittest.TestCase): | ||||
|         person_obj = collection.find_one({'name': 'Test User'}) | ||||
|         self.assertEqual(person_obj['name'], 'Test User') | ||||
|         self.assertEqual(person_obj['age'], 30) | ||||
|         self.assertEqual(person_obj['_id'], person._id) | ||||
|         self.assertEqual(person_obj['_id'], person.id) | ||||
|  | ||||
|     def test_save_custom_id(self): | ||||
|         """Ensure that a document may be saved with a custom _id. | ||||
|         """ | ||||
|         # Create person object and save it to the database | ||||
|         person = self.Person(name='Test User', age=30,  | ||||
|                              _id='497ce96f395f2f052a494fd4') | ||||
|                              id='497ce96f395f2f052a494fd4') | ||||
|         person.save() | ||||
|         # Ensure that the object is in the database with the correct _id | ||||
|         collection = self.db[self.Person._meta['collection']] | ||||
| @@ -268,7 +268,7 @@ class DocumentTest(unittest.TestCase): | ||||
|         post_obj.author.age = 25 | ||||
|         post_obj.author.save() | ||||
|  | ||||
|         author = self.Person.objects(name='Test User').first() | ||||
|         author = list(self.Person.objects(name='Test User'))[-1] | ||||
|         self.assertEqual(author.age, 25) | ||||
|  | ||||
|         BlogPost.drop_collection() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user