doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.88k stars 355 forks source link

Get/Filter by ObjectIdField #598

Closed srPuebla closed 2 years ago

srPuebla commented 2 years ago

One line description of the issue

Sorry but i cant find how can i get or filter by ObjectIdField

Python script

I have the next model


class AuditNames(models.Model):
    _id = models.ObjectIdField()
    name = models.CharField(max_length=256)
    type_name = models.CharField(max_length=256)
    action = models.CharField(max_length=256)

And here i am trying to make the query.

auditid = "60ec1636a6bdc7cf5c710449"
# auditid = ObjectIdField(auditid) # Also i am trying this way but its imposible
audit_name = aUDITnAMES.objects.get(_id = auditid)

Traceback

matching query does not exist.

srPuebla commented 2 years ago

I have just to figure out it.

Just use "ObjectId" of mongo library. from bson.objectid import ObjectId AuditNames.objects.get(_id = ObjectId(auditid))