_id field is not working

38 views
Skip to first unread message

programacion webdecero

unread,
Mar 16, 2022, 1:59:25 PM3/16/22
to djongo
I want to use the _id field of mongodb as primary key, but when I create an object it keeps adding the default 'id' field. This is my model code:

class UserMongo(models.Model):
_id = models.ObjectIdField(primary_key=True, db_column='_id')
username = models.CharField(max_length=25)
email = models.EmailField()
password = models.CharField(max_length=25)
objects = UserMongoManager()
def __str__(self):
return "{} {}".format(self._id, self.username)

And this what I get when creating the object from postman:
{
"_id": 27,
"username": "user13",
"email": "use...@mail.com",
"password": "password13"
}

In compass I see this data:
{
    "_id": {
        "$oid": "6232240214afce2f3a4c0e13"
    },
    "id": 27,
    "username": "user13",
    "email": "use...@mail.com",
    "password": "password13"
}

Reply all
Reply to author
Forward
0 new messages