identixone/fastapi_contrib

Why Serializer do not output "_id" field?

ChandlerBent opened this issue · 1 comments

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

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.