Created
July 26, 2018 12:27
-
-
Save elyssonmr/36c2a541aee999d28e3dc194e234cfbf to your computer and use it in GitHub Desktop.
uMongo unique issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from datetime import datetime | |
from motor.motor_asyncio import AsyncIOMotorClient | |
from umongo import Instance, Document, fields, validate | |
db = AsyncIOMotorClient().test | |
instance = Instance(db) | |
@instance.register | |
class User(Document): | |
email = fields.EmailField(required=True, unique=True) | |
birthday = fields.DateTimeField(validate=validate.Range(min=datetime(1900, 1, 1))) | |
friends = fields.ListField(fields.ReferenceField("User")) | |
class Meta: | |
collection = db.user | |
loop = asyncio.get_event_loop() | |
async def main(): | |
goku = User(email='[email protected]', birthday=datetime(1984, 11, 20)) | |
await goku.commit() | |
vegeta = User(email='[email protected]', friends=[goku]) | |
await vegeta.commit() | |
try: | |
goku2 = User(email='[email protected]', birthday=datetime(1984, 11, 20)) | |
await goku2.commit() | |
except Exception as ex: | |
print(ex) | |
print(vegeta.friends) | |
# <object umongo.data_objects.List([<object umongo.dal.pymongo.PyMongoReference(document=User, pk=ObjectId('5717568613adf27be6363f78'))>])> | |
print(vegeta.dump()) | |
# {id': '570ddb311d41c89cabceeddc', 'email': '[email protected]', friends': ['570ddb2a1d41c89cabceeddb']} | |
data = User.find({"email": '[email protected]'}) | |
async for user in User.find(): | |
print(user) | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment