Skip to main content

A ``PercentageField`` for Django that shows a Decimal (ex. ``0.2100`` in the database - by default rounded to 4 decimal spaces) as a percentage in the admin (ex. ``21%``). With built-in (overridable) min and max validator to ensure a valid percentage range: 0 ≤ x ≤ 100.

Project description

Django Percentage Field

This package add a PercentageField to Django (also works with Unfold, some minor styling improvements needed) that shows a percentage in the admin as 21%, but stores it as a Decimal - 0.2100 - to the database. This makes it clearer for admins what the field represents, while the decimal value makes actual calculations easier.

Installation

Run pip install django-percentagefield (or poetry add django-percentagefield)

Settings

  • add django_percentagefield to your INSTALLED_APPS before django.contrib.admin
  • in your models.py, add the PercentageField as such:
from django.db import models

from django_percentagefield.db.models import PercentageField


class YourModel(models.Model):
    percentage_field = PercentageField()
  • in your admin.py, add the following wherever you use PercentageField:
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from django_percentagefield.utils import format_percentage

from .models import YourModel


@admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
    list_display = ('formatted_percentage_field',)

    def formatted_percentage_field(self, instance: YourModel) -> str:
        return format_percentage(value=instance.percentage_field, include_percentage_symbol=True)

    formatted_percentage_field.short_description = _('PercentageField description')
  • [optional]: add PERCENTAGE_MAX_DIGITS and PERCENTAGE_DECIMAL_PLACES to your settings.py (default values 7 and 4 respectively)

TODO: make the django-percentagefield package handle the formatting in list_display, readonly_fields, etc. automatically.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_percentagefield-0.1.4.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_percentagefield-0.1.4-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file django_percentagefield-0.1.4.tar.gz.

File metadata

  • Download URL: django_percentagefield-0.1.4.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for django_percentagefield-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3b25fb93d7fc481fca5745a641fd40acd2682a554f1da2b8f3bbc77457930937
MD5 cbe4ebbe46e8f25949c0af1a393d3483
BLAKE2b-256 4d6ff47f125f7e1edfb66f5e78c9e7c6aaef428c90a5c057145b73d36cc22fc4

See more details on using hashes here.

File details

Details for the file django_percentagefield-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_percentagefield-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1fa06ab616f8d0d29eec4ca0390fefbe6ab6ac15c0fe8ef91073d3d2cd4fb74b
MD5 f20b66f9f523268c92469ec415711b41
BLAKE2b-256 903f44e581017d4066012385ad6acbfdf4bbe4b34c7900bae1cf2d8cc4e1c69e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page