{"id":21816592,"url":"https://github.com/5monkeys/django-traits","last_synced_at":"2025-04-14T01:15:29.240Z","repository":{"id":46493475,"uuid":"414174210","full_name":"5monkeys/django-traits","owner":"5monkeys","description":null,"archived":false,"fork":false,"pushed_at":"2021-10-12T13:12:28.000Z","size":39,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-06T10:02:19.781Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-traits/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/5monkeys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-06T10:54:32.000Z","updated_at":"2023-03-15T23:36:31.000Z","dependencies_parsed_at":"2022-09-26T18:01:16.699Z","dependency_job_id":null,"html_url":"https://github.com/5monkeys/django-traits","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fdjango-traits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fdjango-traits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fdjango-traits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fdjango-traits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/5monkeys","download_url":"https://codeload.github.com/5monkeys/django-traits/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804824,"owners_count":21164135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-27T15:35:27.036Z","updated_at":"2025-04-14T01:15:29.222Z","avatar_url":"https://github.com/5monkeys.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=center\u003e\u003cimg src=docs/logo.svg width=200/\u003e\u003c/p\u003e\n\n\n\u003ch1 align=center\u003edjango-traits\u003c/h1\u003e\n\n\u003cp align=center\u003e\n    \u003ca href=https://github.com/5monkeys/django-traits/actions?query=workflow%3ACI+branch%3Amain\u003e\u003cimg src=https://github.com/5monkeys/django-traits/workflows/CI/badge.svg alt=\"CI Build Status\"\u003e\u003c/a\u003e\n    \u003ca href=https://codecov.io/gh/5monkeys/django-traits\u003e\u003cimg src=\"https://codecov.io/gh/5monkeys/django-traits/branch/main/graph/badge.svg?token=U6BK5DWAWD\" alt=\"Test coverage reports\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nDefine traits for Django models that works seamlessly both in-Python and using the ORM,\nwith coordinated tests.\n\n### Installation\n\n```bash\n$ python3 -m pip install django-traits\n```\n\n### Example\n\n```python\nclass Rich(Trait[\"Person\"]):\n    q = models.Q(income__gt=1000)\n\n    def check_instance(self, instance: Person) -\u003e bool:\n        return instance.income \u003e 1000\n\n\nclass Person(models.Model):\n    is_rich = Rich()\n    income = models.PositiveIntegerField()\n\n\n# Filter for rich people, this uses the ORM predicate as defined in q.\nrich_people = Person.is_rich.all()\n\n# Check if a person is rich, this uses the in-Python predicate as defined in check_instance().\nperson = Person.objects.first()\nif person.is_rich:\n    print(\"Money is not a problem\")\nelse:\n    print(\"This person is not rich\")\n```\n\nThe automated test factory makes it simple to write tests that guarantees that the\nin-Python predicate stays in sync with its ORM counterpart. The following example\ngenerates parameterized tests that checks boundary values.\n\n\n\n```python\nimport pytest\nfrom traits.tests import create_trait_test\n\n\nparametrize_people = pytest.mark.parametrize(\n    \"instance\",\n    PersonFactory(),\n    PersonFactory(income=1000),\n    PersonFactory(income=1001),\n)\n\n\nclass TestPerson:\n    test_is_rich = parametrize_people(create_trait_test(Person.is_rich))\n```\n\nThe above example will generate three tests that checks that each given instance of\n`Person` only appears in an ORM query result if the trait evaluates to `True` for that\ninstance, and that the result set is empty if it evaluates to `False`. So if the\nimplementations were to drift apart, for instance if the limit was increased to `2000`\nin `check_instance()` but not in `q`, the tests would fail and enforce that the two\nimplementations are in sync.\n\nYou can also use [Hypothesis] to automatically generate test values.\n\n```python\nfrom traits.tests import create_trait_test\nfrom hypothesis import given\nfrom hypothesis.strategies import builds\nfrom hypothesis.strategies import integers\n\npeople = builds(\n    PersonFactory.build,\n    income=integers(-9223372036854775808, 9223372036854775807),\n)\n\n\nclass TestPerson:\n    test_is_rich = given(people)(create_trait_test(Person.is_rich))\n```\n\n\n[Hypothesis]: https://github.com/HypothesisWorks/hypothesis\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fdjango-traits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F5monkeys%2Fdjango-traits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fdjango-traits/lists"}