User.objects.filter(email__icontains='something')
这里的情况是电子邮件字段是一个自定义字段,此类存储数据库中的加密值,并在需要调用对象时再次解密该值
class AESEncryptedField(models.TextField):
def get_prep_value(self, value):
if value is not None:
return cryptography.encrypt(value)
else:
return None
def from_db_value(self, value, expression, connection, context):
try:
return cryptography.decrypt(
super(AESEncryptedField, self).to_python(value)
)
except TypeError:
"""
Field isnt encrypted on the database, but it will be
on the first model.save()
"""
return super(AESEncryptedField, self).to_python(value)
def formfield(self, **kwargs):
defaults = {'max_length': self.max_length, 'widget': forms.TextInput}
defaults.update(kwargs)
return super(AESEncryptedField, self).formfield(**defaults)
这是模型
class User(AbstractBaseUser):
email = AESEncryptedField(
verbose_name='correo',
max_length=255,
unique=True
)
执行此查询时工作正常
User.objects.filter(email='something@example.com')
但是当使用email__icontains条件不起作用时,结果为空。
有人知道是否需要覆盖某种方法?
提前感谢!!