Django field and widget for rule-engine with visual editor
Project description
Django rule-engine - Field, Widget & API
A powerful Django field and widget that brings rule-engine to your Django projects with a beautiful visual editor, real-time validation, and a clean API.
Perfect for building flexible business rules, access control policies, discount conditions, validation rules, and any scenario where you need dynamic, user-configurable logic.
✨ Key Features
🎨 Visual Rule Editor
- Syntax highlighting for better readability
- Real-time validation with immediate feedback
- Auto-formatting for JSON example data
- Keyboard shortcuts (Ctrl+Enter to validate)
- Dark mode support with modern UI
🔌 Easy Integration
from django_rule_engine.fields import RuleField
class MyModel(models.Model):
access_rule = RuleField(
verbose_name="Access Rule",
example_data={
"age": 25,
"status": "active",
"role": "admin"
}
)
⚡ REST API
Built-in validation endpoint for frontend validation:
POST /api/validate-rule/
🎯 Django Admin Integration
Seamlessly integrates with Django Admin - just add the field and the editor appears automatically!
📦 Installation
pip install django-rule-engine
Add to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'django_rule_engine',
]
Include URLs:
urlpatterns = [
path('', include('django_rule_engine.urls')),
]
💡 Use Cases
- ✅ Access Control: Define who can access resources
- ✅ Business Rules: Implement complex discount/pricing logic
- ✅ Validation: Create dynamic validation rules
- ✅ Eligibility: Determine program/service eligibility
- ✅ Classification: Classify entities based on criteria
- ✅ Filtering: Advanced data filtering and queries
🎓 Quick Example
from django.db import models
from django_rule_engine.fields import RuleField
class DiscountCampaign(models.Model):
name = models.CharField(max_length=200)
rule = RuleField(
verbose_name="Discount Rule",
example_data={
"price": 150.00,
"quantity": 3,
"customer_type": "premium",
"first_purchase": False
},
help_text="Example: price > 100 and quantity >= 2"
)
discount_percentage = models.DecimalField(max_digits=5, decimal_places=2)
def calculate_discount(self, order_data):
"""Calculate discount based on the rule."""
if not self.rule:
return 0
import rule_engine
rule = rule_engine.Rule(self.rule)
if rule.matches(order_data):
return float(self.discount_percentage)
return 0
📚 Documentation
Comprehensive documentation is included in the package:
- INSTALL.md - Installation and setup guide
- QUICKSTART.md - Get started in 5 minutes
- EXAMPLES.md - Real-world code examples
- MIGRATION_GUIDE.md - Migrate existing projects
- VISUAL_DEMO.md - Visual demonstration
- INDEX.md - Complete documentation index
🔧 Requirements
- Python >= 3.10
- Django >= 4.2
- rule-engine >= 4.5.3
- django-json-widget >= 2.0
🎯 Tested On
- Python: 3.10, 3.11, 3.12, 3.13, 3.14
- Django: 4.2, 5.0, 5.1, 5.2
- Modern browsers (Chrome, Firefox, Safari, Edge)
Validation API
REST endpoint for dynamically validating rule-engine rules.
- Endpoint:
POST /api/validate-rule/ - Documentation: fields/README.md
🚀 Quick Start
1. Import and Use
from django_rule_engine.fields import RuleField
class MyModel(models.Model):
rule = RuleField(
example_data={"age": 25, "status": "active"}
)
📚 Documentation
- INDEX.md - Complete documentation index
- INSTALL.md - Installation guide
- QUICKSTART.md - Quick start (5 min)
- MIGRATION_GUIDE.md - Migration guide
- EXAMPLES.md - Code examples
- VISUAL_DEMO.py - Demonstração Visual
🎯 Implementation Example
The field is already being used in the Cohort model in coorte/models.py:
class Cohort(Model):
name = CharField("cohort name", max_length=256, unique=True)
rule = RuleField(
"validation rule",
blank=True,
null=True,
example_data={
"login": "user123",
"user": {"email": "user@example.com"},
"name": "Zé da Silva",
"active": True
},
default="login == 'user123' and user.email != 'user123@example.com'",
)
class Meta:
verbose_name = _("cohort")
verbose_name_plural = _("cohorts")
ordering = ["name"]
def __str__(self):
return self.name
Project details
Release history Release notifications | RSS feed
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_rule_engine-1.0.1.tar.gz.
File metadata
- Download URL: django_rule_engine-1.0.1.tar.gz
- Upload date:
- Size: 29.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e85c7f4e39bccb872e355a853b558ccdba5bd2946bf3c595a689ffaed03d182
|
|
| MD5 |
1a79ecd73f53b786538682d238852548
|
|
| BLAKE2b-256 |
310496fc3f60f2c6f22357fefcb5bcdf32d36ae136f3b24d12e40189df98f675
|
Provenance
The following attestation bundles were made for django_rule_engine-1.0.1.tar.gz:
Publisher:
publish-to-pypi.yml on kelsoncm/django-rule-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rule_engine-1.0.1.tar.gz -
Subject digest:
1e85c7f4e39bccb872e355a853b558ccdba5bd2946bf3c595a689ffaed03d182 - Sigstore transparency entry: 906043079
- Sigstore integration time:
-
Permalink:
kelsoncm/django-rule-engine@40151318a496bfc3c44098af7fe2b5425cf40123 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/kelsoncm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@40151318a496bfc3c44098af7fe2b5425cf40123 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_rule_engine-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_rule_engine-1.0.1-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb80dc2bcec193fc2ba84bd942fecc3058ad3330f8cc2fa3e8d623374ec86b8f
|
|
| MD5 |
effb6fda57982947b48911db6b981adb
|
|
| BLAKE2b-256 |
a6730237d17110b9e51558175f25f7d6ea3083a0d4efbccba4abf159335aed1c
|
Provenance
The following attestation bundles were made for django_rule_engine-1.0.1-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on kelsoncm/django-rule-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rule_engine-1.0.1-py3-none-any.whl -
Subject digest:
eb80dc2bcec193fc2ba84bd942fecc3058ad3330f8cc2fa3e8d623374ec86b8f - Sigstore transparency entry: 906043122
- Sigstore integration time:
-
Permalink:
kelsoncm/django-rule-engine@40151318a496bfc3c44098af7fe2b5425cf40123 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/kelsoncm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@40151318a496bfc3c44098af7fe2b5425cf40123 -
Trigger Event:
release
-
Statement type: