Skip to main content

django-traits

CI Build Status

Define traits for Django models that works seamlessly both in-Python and using the ORM, with coordinated tests.

Installation

$ python3 -m pip install django-traits

Example

class Rich(Trait["Person"]):
    q = models.Q(income__gt=1000)

    def check_instance(self, instance: Person) -> bool:
        return instance.income > 1000


class Person(models.Model):
    is_rich = Rich()
    income = models.PositiveIntegerField()


# Filter for rich people, this uses the ORM predicate as defined in q.
rich_people = Person.is_rich.all()

# Check if a person is rich, this uses the in-Python predicate as defined in check_instance().
person = Person.objects.first()
if person.is_rich:
    print("Money is not a problem")
else:
    print("This person is not rich")

The automated test factory makes it simple to write tests that guarantees that the in-Python predicate stays in sync with its ORM counterpart.

class TestPerson:
    test_is_rich = test_trait(
        (
            (PersonFactory(), False),
            (PersonFactory(income=1000), False),
            (PersonFactory(income=1001), True),
        )
    )

The above example will generate tests that exercises the in-Python predicate on instances of Person, as well as tests that tries to filter using the ORM predicate. That means that if the implementations were to drift apart, for instance if the limit was increased to 2000 in check_instance() but not in q, the tests would fail.

Metadata

Release files for django-traits 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-traits 0.0.1
File Size Uploaded
django-traits-0.0.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-traits 0.0.1
File Interpreter ABI Platform
django_traits-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / django-traits-0.0.1.tar.gz

Download URL django-traits-0.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7f6a1c3e7d84f7923218c895aa8480928aca910da1675e1a2dc50a8131b8aeb2
BLAKE2b-256 checksum
How to use checksums
28c72ec46613a03e18286aa26dc2a3ab9384ecf9a5d9f2a5aa9d645117bd756c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / django_traits-0.0.1-py3-none-any.whl

Download URL django_traits-0.0.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
484fb5d49c2645c5ecd1896889d4cd3d2c5c203d1d3e1b0fd588fc5972ba091f
BLAKE2b-256 checksum
How to use checksums
69c32b925bf87fe8ec5631e093d5322236606449bdb706d65e1b29da8b5e6a02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page