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

Why Serializer do not output "_id" field? #170

Open
ChandlerBent opened this issue May 10, 2021 · 1 comment
Open

Why Serializer do not output "_id" field? #170

ChandlerBent opened this issue May 10, 2021 · 1 comment

Comments

@ChandlerBent
Copy link

My model need output _id field to frontend of my project.

    def dict(self, *args, **kwargs) -> dict:
        """
        Removes excluded fields based on `Meta` and `kwargs`
        :return: dict of serializer data fields
        """
        exclude = kwargs.get("exclude")
        if not exclude:
            exclude = set()

        **exclude.update({"_id"})**

        if hasattr(self.Meta, "exclude") and self.Meta.exclude:
            exclude.update(self.Meta.exclude)

        if (
            hasattr(self.Meta, "write_only_fields")
            and self.Meta.write_only_fields
        ):
            exclude.update(self.Meta.write_only_fields)

        kwargs.update({"exclude": exclude})
        original = super().dict(*args, **kwargs)
        return original
@levchik
Copy link
Contributor

levchik commented Jun 3, 2021

This design decision was really long time ago, but from what I can remember, it was chosen because id looked cleaner to me as a key for identifier values & not to leak information that underlying DB in such project is MongoDB.

Obviously, to be more versatile library we could support both id and _id. I think it is quite trivial to implement such a feature, so if you're willing I could review PR.

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

2 participants