I'm using djongo in order to connect my Django Rest Framework project to MongoDB,
As MongoDB using _id
as its primary key, I changed my models like this:
from djongo import models
class MyModel(models.Model):
_id = models.ObjectIdField(primary_key=True)
name = models.CharField(max_length=200)
When I query data from database, the type of _id
will be bson.objectid.ObjectId
So any time I want to query anything, I should do it like this:
from bson.objectid import ObjectId
from myapp.models import MyModel
MyModel.objects.get(pk=ObjectId('606e91bd371197379e9bf919'))
# or
MyModel.objects.filter(_id=ObjectId('606e91bd371197379e9bf919'))
and if I use MyModel.objects.filter(pk='606e91bd371197379e9bf919')
it can't find the data in database
This additional ObjectId
which is required for any query makes many problems because none of the pre-existing classes (like Forms, Views, Django Rest Framework Serializers, ....) act like that, so I should override all of them, and this cause many code duplications,
Is there a way to override any of Q
/QuerySet
/Model
/Model Meta Class
or a wrapper or anything else to apply the additional ObjectId
for querying pk
/id
/_id
field and solve this problem?
Is a better way to do this?