georgemarshall / django-cryptography

Easily encrypt data in Django
https://django-cryptography.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
371 stars 69 forks source link

FieldError: Unsupported lookup 'exact' for EncryptedCharField #84

Closed ashrafZolkopli closed 1 year ago

ashrafZolkopli commented 1 year ago

Hi all,

I have to first say thanks to everyone who contributed and gave this wonderful app for us mere mortal to use and help ease the process of encrypting data at the database level.

Anyway today I just started a new project using: python 3.10 django 4.0.6 django-cryptography 1.1

usually I can wrap my field with encrypt using

class Detail(models.Model):
    nric= encrypt(
        models.CharField(
            _("NRIC"),
            max_length=12,
            validators=[

                RegexValidator(
                    regex="^[0-9]{12}$",
                    message=_("NRIC must be numbers only")
                ),
                MinLengthValidator(
                    limit_value=12,
                    message=_("NRIC  must be 12 digit only")
                ),
                MaxLengthValidator(
                    limit_value=12,
                    message=_("NRIC  must be 12 digit only")
                )
            ]
        )
    )

however when i am trying to query using the following statement

def query_detail(nric : str):
    detail : Detail | None = Detail.objects.filter(nric = nric ).first()

the following error was raise

FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'exact' for EncryptedCharField or join on the field not permitted, perhaps you meant exact or iexact?

any help on how to debug this issue would be great. However, i dont mind changing library if I have too

BTW: Django admin works just fine

Thanks Ash

HectorGitt commented 1 year ago

I have the same problem.

python 3.10.7 django 4.0.7 django-cryptography 1.1

twitter_auth_token = TwitterAuthToken.objects.filter(oauth_token=oauth_token).first()

Unsupported lookup 'exact' for EncryptedCharField or join on the field not permitted, perhaps you meant exact or iexact?

georgemarshall commented 1 year ago

This is intentional behavior as values are encrypted when saved to the database. There is no way hash the values without compromising the level of security.

If you need to match on a value, you'll have to query everything and iterate through the values. Matching what you are looking for in your code.