{"id":24300128,"url":"https://github.com/pwhipp/python_metaclass","last_synced_at":"2026-05-29T12:04:11.220Z","repository":{"id":70783519,"uuid":"110486156","full_name":"pwhipp/python_metaclass","owner":"pwhipp","description":"Working example showing some of the power of python3 metaclasses","archived":false,"fork":false,"pushed_at":"2017-11-13T02:46:33.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T12:47:40.551Z","etag":null,"topics":["example","metaclass","python3"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pwhipp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-13T01:39:06.000Z","updated_at":"2020-02-19T03:28:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"a59886ee-433f-4b99-9af0-b8941deb3033","html_url":"https://github.com/pwhipp/python_metaclass","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pwhipp/python_metaclass","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwhipp%2Fpython_metaclass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwhipp%2Fpython_metaclass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwhipp%2Fpython_metaclass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwhipp%2Fpython_metaclass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pwhipp","download_url":"https://codeload.github.com/pwhipp/python_metaclass/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwhipp%2Fpython_metaclass/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33650712,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["example","metaclass","python3"],"created_at":"2025-01-16T22:49:53.654Z","updated_at":"2026-05-29T12:04:11.216Z","avatar_url":"https://github.com/pwhipp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python3 Metaclass Example\n\nWe need something a little meaningful so the example consists of some simple models in a class heirarchy and an associated heirarchy of model serializers\u003csup id=\"n1\"\u003e[1](#f1).\n\nThe [first commit](https://github.com/pwhipp/python_metaclass/commit/97d3b9aaa950f543f77d23dae3f633e5845543ca) shows how serializers need to be accessed for the model classes in order to serialize/deserialize model data.\n\nMaintaining the serializer_class_map quickly becomes cumbersome.\n\nWe could define a decorator e.g.\n```python\n@SetModelClass(ModelSerializer)\nclass ModelSerializer(Serializer):\n    ...\n```\n\nbut this is repeating the association because its already present in the Meta (so that the serializer can create new models)\n\nWe could add a serializer_class field to the models but this is coupling the serializers to the models in an undesirable way. We want to keep the serialization concern to our serializers.\n\n## So how about a metaclass?\nIf we specify a metaclass for our serializers we can update the serializer_class_map whenever we create a new serializer class.\n\n[This commit](https://github.com/pwhipp/python_metaclass/commit/f1578ba6a66c40e9e15547c94980a68a11ca07d0) shows the use of the `MappedSerializerClass` to maintain the serializer_class_map. This separates out the mapping concern and is a pretty good solution.\n\n## Footnotes\n\n\u003cb id=\"f1\"\u003e1\u003c/b\u003e This is a loose abstraction of [django models](https://docs.djangoproject.com/en/1.11/topics/db/models/) and the [django rest framework](http://www.django-rest-framework.org/). [↩](#n1)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwhipp%2Fpython_metaclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpwhipp%2Fpython_metaclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwhipp%2Fpython_metaclass/lists"}