Skip to content
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

Closed
ianjuma opened this issue Feb 15, 2014 · 9 comments
Closed

Comments

@ianjuma
Copy link

ianjuma commented Feb 15, 2014

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?
class Member(Document):
    # Member obj definition - attrs
    meta = {'collection': 'member'}
    meta = {'allow_inheritance': True}

class Physician(Member):
    """ Physician obj deps - inheritance -> Member

    """
    meta = {'collection': 'physician'}
@techiev2
Copy link

Physician, deriving from Member would be present in the collection named member.

[edit]
Internally though, the _cls attribute in the document would show Member.Physician

{'_cls': 'Member.Physician'}

[/edit]

Specifying a collection name in a subclass would raise a warning while using just the allow_inheritance meta key.

Trying to set a collection on a subclass (Physician)

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
    }

@ianjuma
Copy link
Author

ianjuma commented Feb 20, 2014

the meta data fixed the syntax warning issue, thanks! Could this be included in the docs?

@ianjuma ianjuma closed this as completed Feb 20, 2014
@sit-in
Copy link

sit-in commented Jan 26, 2015

fix my problem ,thank you !

@thedrow
Copy link
Contributor

thedrow commented Jan 26, 2015

@ianjuma Can you open a separate issue about the documentation bug please?

@meet-gandhi
Copy link

fixed my problem. Thanks

@pratibhajagnere
Copy link

Thank you @techiev2 , using your suggestion, I am able to make my class abstract.

@cicada-lewis
Copy link

But if I want to make the parent class like Member saved in a collection, what should I do? Please @techiev2

@techiev2
Copy link

techiev2 commented Sep 14, 2018

@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__.__bases__ property like any other Python class. You'd have to override the __init__ in the derived class for this and set the _cls property manually after init.

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 __init__ override flow a bit; use the self._cls value for self._meta["collection"]

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.

@ikhwanperwira
Copy link

ikhwanperwira commented May 11, 2023

@techiev2

After override collection with meta, I save the collection with

p.save(validate=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants