[Question]how to disable validation on objectId field?

321 views
Skip to first unread message

Wenyao Xue

unread,
May 5, 2015, 10:48:38 PM5/5/15
to mongoeng...@googlegroups.com
I recently take over a project, which uses django and mongodb. The "_id" in the mongodb database is not normal objectId type. Therefore, when querying the database using monogoegine, it throws an exception:

u'\u664bF779BS-460001241632524-86627801555819' is not a valid ObjectId, it must be a 12-byte input of type 'str' or a 24-character hex string

Can I disable the valication of the _id field?

Matthew Ellison

unread,
May 6, 2015, 8:52:51 AM5/6/15
to mongoeng...@googlegroups.com

Hello,

I believe you should be able to set a different field as your primary key. For example try:

class MyDocument(Document):
    id = DynamicField(primary_key=True, db_field="_id")

That should let you store any type of value as the ID if you’re only interested in querying the data. It would probably be a better idea to create a proper Field type for your primary key and add a custom validator.

Hope this help!

Matthew Ellison


--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages