KField custom model fieldΒΆ

The django-kmatch app comes with a KField custom model field that can be used to store kmatch patterns and obtain associated K objects. An example of using the field in a model definition is below:

from django.db import models
from django_kmatch import KField

class KModel(models.Model):
    k = KField()

Using the KField is done like the following:

# Create a KModel object with a pattern to match dictionaries with a 'a' field of 2
k_model = KModel.objects.create(k=['==', 'a', 2])

# Match dictionaries with the K object returned from the KField
print k_model.k.match({'a': 2})
True

The KField field also does validation of the pattern:

# Create a KModel with an invalid pattern
k_model = KModel.objects.create(k='invalid')
Traceback (most recent call last):
    # Traceback message here ...
ValueError: Not a valid operator or filter - 'invalid'

Nullable KField values are fine as well.