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_percentagefieldto yourINSTALLED_APPSbeforedjango.contrib.admin - in your
models.py, add thePercentageFieldas 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 usePercentageField:
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_DIGITSandPERCENTAGE_DECIMAL_PLACESto yoursettings.py(default values7and4respectively)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_percentagefield-0.1.6.tar.gz.
File metadata
- Download URL: django_percentagefield-0.1.6.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
774ff1486c8dc817cc9f77ea00e9a8effac5aa737c13507343c1d6e2b8480848
|
|
| MD5 |
2fd87e70efcd17f02d08d90b11227fc6
|
|
| BLAKE2b-256 |
415e2959c5ac0a31ff7db4756d2d929f53b6a1e69b1ba48f7e857b3985a57fee
|
File details
Details for the file django_percentagefield-0.1.6-py3-none-any.whl.
File metadata
- Download URL: django_percentagefield-0.1.6-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4616640c3534ac3670f87d8a8508853459f2190741082231feff3b1e4036be7
|
|
| MD5 |
ba218f0998b2e55581a82ccb05d79e54
|
|
| BLAKE2b-256 |
fb4934c6317a31170e4c09d6d1d8dd9b120a04a047a7d083bcec1a6ea10cb07d
|