Skip to main content

Django Annotated Property

Decorator for Django model properties that returns annotated values when available, avoiding database queries (f.e. O(N) vs O(1) for N items).

If a queryset includes annotated_<property>, the decorator uses it instead of calling the original method.

Example

from django.db import models
from django_annotated_property.annotated_property import annotated_property

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=8, decimal_places=2)

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()

    @annotated_property
    def total_price(self):
        return self.product.price * self.quantity

now you can use the total_price property in your views or templates:

items = (
    OrderItem.objects
    .annotate(annotated_total_price=models.F('product__price') * models.F('quantity'))
)
for item in items:
    print(item.total_price)  # returns annotated_total_price instead of the original method

TODO

  • Add tests

Release files for django-annotated-property 0.1.0

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-annotated-property 0.1.0
File Size Uploaded
django_annotated_property-0.1.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-annotated-property 0.1.0
File Interpreter ABI Platform
django_annotated_property-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 kB

Release files / django_annotated_property-0.1.0.tar.gz

Download URL django_annotated_property-0.1.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a3f1536b431de0ec16c80b405d2452ffbf00cd230317e465d85998e26af9c3ed
BLAKE2b-256 checksum
How to use checksums
2a53e52b3a851bd8ef742d3662fd224760918c6b701e927a9958c9c851a488be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release files / django_annotated_property-0.1.0-py3-none-any.whl

Download URL django_annotated_property-0.1.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fad359e48665bd5a41c61d6c26fd9da8713260fbc2e731bb4cc1fd5e83e6d4cb
BLAKE2b-256 checksum
How to use checksums
bda754696c2cd83b35e7805d818cd0082647226bda5665824f183f335472cda2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

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