Custom "id" field for all Documents

196 views
Skip to first unread message

Trevor Keith

unread,
Aug 8, 2013, 1:27:19 PM8/8/13
to mongoeng...@googlegroups.com
Hello,

I'm trying to figure out how to change the type of the _id field (which I believe is just "id" in MongoEngine) for all my Documents. I tried subclassing Document, but of course that doesn't work because then subclasses of my subclass will all go into the same collection. I also tried using a separate class that I subclass along with Document, but Document's "id" field still seems to override it. What's the best way to use a custom id field for all my Documents?

Thanks,
Trevor

Hans Roman

unread,
Aug 8, 2013, 4:29:59 PM8/8/13
to mongoeng...@googlegroups.com
Your "id" field can be any type, but you'd have to set "primary_key" to True in that field. For instance:


class Foo(Document):
    id = StringField(primary_key=True)
    ...

Jan Schrewe

unread,
Aug 9, 2013, 8:04:16 AM8/9/13
to mongoeng...@googlegroups.com
Or you can name the "id" field what you want and access it through the "pk" attribute. So this works too:

class Foo(Document):
    pantoffel_kartoffel = StringField(primary_key=True)
    ...

f = Foo.objects()[0]
print(f.pk)


--
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/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages