Skip to main content

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:

🔧 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.

🚀 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

  1. INDEX.md - Complete documentation index
  2. INSTALL.md - Installation guide
  3. QUICKSTART.md - Quick start (5 min)
  4. MIGRATION_GUIDE.md - Migration guide
  5. EXAMPLES.md - Code examples
  6. 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


Download files

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

Source Distribution

django_rule_engine-1.0.1.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

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

django_rule_engine-1.0.1-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

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

Hashes for django_rule_engine-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1e85c7f4e39bccb872e355a853b558ccdba5bd2946bf3c595a689ffaed03d182
MD5 1a79ecd73f53b786538682d238852548
BLAKE2b-256 310496fc3f60f2c6f22357fefcb5bcdf32d36ae136f3b24d12e40189df98f675

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_rule_engine-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rule_engine-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb80dc2bcec193fc2ba84bd942fecc3058ad3330f8cc2fa3e8d623374ec86b8f
MD5 effb6fda57982947b48911db6b981adb
BLAKE2b-256 a6730237d17110b9e51558175f25f7d6ea3083a0d4efbccba4abf159335aed1c

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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