About Assigning Colors to Model Instances
Here's a technique to assign a color to a model instance based on the hash value of their ID:
COLORS = [
"#3B82F6",
"#8B5CF6",
"#EC4899",
]
def get_color(instance):
import hashlib
instance_id = str(instance.pk)
hash_value = int(hashlib.md5(instance_id.encode()).hexdigest(), 16)
return COLORS[hash_value % len(COLORS)]
Tips and Tricks Programming Development Django 5.2 Django 4.2 Python 3
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django App for You
Django GDPR Cookie Consent app
For Django websites that use cookies.
Django App for You