数据未保存为加密数据Django [英] Data is not being saved as Encrypted data django

查看:59
本文介绍了数据未保存为加密数据Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我尝试了6个以上的插件,但现在却感到非常沮丧.现在使用此密码学

till now I tried more then 6 plugins and quite frustrate now. Now using this one cryprtography

一切都很好,并做了相应的工作,但是当我将数据保存在这样的模型管理器中

everything is fine and done accordingly but when I save data in model manager like this

 def create_user(self, email, password, **extra_fields):
        user = self.model(email=email, **extra_fields)
        user.test_field = 'new.user@oc.com'
        user.save(using=self._db)
        return user

它保存通常不加密的数据

it saving data normally not encrypted

我的模特很像

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    test_field = encrypt(models.CharField(max_length=100))
    objects = UserManager()

推荐答案

您似乎正在按预期进行所有操作.

It looks like you are doing everything as expected.

数据应在数据库端加密.

Data should be encrypted on the Database end.

但是,在Django端以明文形式查看数据是正常的,因为ORM无缝地对其进行了解密.

However, it's normal to see the data in clear text on Django side because the ORM decrypt it seamlessly.

如果直接检查数据库中的数据(使用不带ORM的原始SQL查询),则应该看到加密的数据.

If you check the data directly on the database (using raw SQL query without an ORM), you should see encrypted data.

如果需要过滤加密的数据,则应在ORM解密后在python中进行处理:

If you need to filter encrypted data, you should do it in python, after ORM decryption :

除了执行 User.objects.filter(test_field__contains ="somedata")之外,您还需要执行 [User.users.all()中的user用户,如果"somedata"在user.test_field] 中.这种方法的最大缺点是您需要将所有行都传递到ORM(和解密机制)中

Instead of doing User.objects.filter(test_field__contains="somedata"), you will need to do [user for user in User.objects.all() if "somedata" in user.test_field]. The (big) drawback of this method is that you will need to pass all rows into the ORM (and decryption mecanism)

这篇关于数据未保存为加密数据Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆