find() and findOne() in mongoengine

For find() you can do: Cars.objects(model=2013) And for find_one() you can do: Cars.objects.get(model=2013) To retrieve a result that should be unique in the collection, use get(). This will raise DoesNotExist if no document matches the query, and MultipleObjectsReturned if more than one document matched the query. Else if multiple records exists, simply limit, like: Cars.objects(model=2013)[0]

Mongoengine creation_time attribute in Document

You could override the save method. class MyModel(mongoengine.Document): creation_date = mongo.DateTimeField() modified_date = mongo.DateTimeField(default=datetime.datetime.now) def save(self, *args, **kwargs): if not self.creation_date: self.creation_date = datetime.datetime.now() self.modified_date = datetime.datetime.now() return super(MyModel, self).save(*args, **kwargs)

Sort using MongoEngine?

Mongoengine is inspired by Django’s ORM, and like Django, it uses order_by to sort the result set. order_by takes a variable number of string arguments, which are the field names (as defined in your documents) optionally preceded by a “-” (to indicate a descending sort, i.e. highest first). For example: class Person(Document): first_name = StringField() … Read more

PyMongo vs MongoEngine for Django

This is an old question but stumbling across it, I don’t think the accepted answer answers the question. The question wasn’t “What is MongoEngine?” – it was “Why should I use MongoEngine?” And the advantages of such an approach. This goes beyond Django to Python/Mongo in general. My two cents: While both PyMongo and MongoEngine … Read more

Flask throwing ‘working outside of request context’ when starting sub thread

Wrap your thread code in a test_request_context so you have access to context locals: @app.route(‘/my_endpoint’, methods=[‘POST’]) def my_endpoint_handler(): #do tracking in sub-thread so we don’t hold up the page def handle_sub_view(req): with app.test_request_context(): from flask import request request = req # Do Expensive work thread.start_new_thread(handle_sub_view, (request)) return “Thanks” Edit: it’s worth pointing out that the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)