-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxWarning: Trying to set a collection on a subclass (Document-name) #574
Comments
Physician, deriving from Member would be present in the collection named member. [edit] {'_cls': 'Member.Physician'} [/edit] Specifying a collection name in a subclass would raise a warning while using just the allow_inheritance meta key.
It can be overcome by making the base class an abstract class and then specifying the child classes' collection name in their respective meta data. class Member(Document):
# Base class
meta = {
'allow_inheritance': True,
'abstract': True
}
class Physician(Member):
# Derived class
meta = {
'collection': 'Physician' # collection name would be physician
} |
the meta data fixed the syntax warning issue, thanks! Could this be included in the docs? |
fix my problem ,thank you ! |
@ianjuma Can you open a separate issue about the documentation bug please? |
fixed my problem. Thanks |
Thank you @techiev2 , using your suggestion, I am able to make my class abstract. |
But if I want to make the parent class like Member saved in a collection, what should I do? Please @techiev2 |
@Catelemmon, if I have understood your question right, (i.e, you want to add the parent class name to the current class name in the documents in the collection), you can employ the class Member(Document):
# Base class
meta = {
'allow_inheritance': True,
'abstract': True
}
class Physician(Member):
# Derived class
name = mongoengine.StringField()
def __init__(self, *args, **kwargs):
base_classes = self.__class__.__bases__ or ()
base_class_names = ".".join(_.__name__ for _ in base_classes)
super(Physician, self).__init__(*args, **kwargs)
self._cls = "{}.{}".format(base_class_names, self.__class__.__name__).strip(".")
self._meta.update({"collection": self.__class__.__name__}) This would save the _cls as Member.Physician in the saved documents. p = Physician(name="John Doe")
p.save() > db.Physician.find()
{ "_id" : ObjectId("5b9b99479fca0922839086b3"), "_cls" : "Member.Physician", "name" : "John Doe" } Or, if you want to have your collection name as Member.Physician instead, change the If this is not what you are looking at, I'd be able to suggest an alternative if you can provide an indication of where you want the parent class to be stored in the collection. |
After override collection with meta, I save the collection with
|
What collection will physician be saved on? How does one specify a collection on a subclass?
full source
Is there a way to specify a different collection on the Physician document?
The text was updated successfully, but these errors were encountered: