工作笔记-django开发项目-数据库字段加密的问题


先备注一个不相干的指令: 变更用户密码: python manage.py changepassword username

一、 安装package
pip install django-cryptography


二、创建model
from django.db import models

from django_cryptography.fields import encrypt


class MyModel(models.Model):
    name = models.CharField(max_length=50)
    sensitive_data = encrypt(models.CharField(max_length=50))

三、可以像正常model访问一样,使用上面model

问题:

   加密的字段

 支持: MyModel.Object.all().filter()  
不支持 annotate,aggregate不支持,

工作中,要根据前4项的评分合计,乘以权重,得到实际考核评分。
w_attitude = encrypt(models.DecimalField(default=0, max_digits=4, decimal_places=0, verbose_name='工作态度'))
    w_quality = encrypt(models.DecimalField(default=0, max_digits=4, decimal_places=0, verbose_name='工作质量'))
    w_timing = encrypt(models.DecimalField(default=0, max_digits=4, decimal_places=0, verbose_name='工作及时性'))
    w_workload = encrypt(models.DecimalField(default=0, max_digits=4, decimal_places=0, verbose_name='工作饱满度'))
    sum_all = encrypt(models.DecimalField(default=0, max_digits=4, null=True, blank=True, decimal_places=1, verbose_name='总分'))
    quanzhong = models.DecimalField(default=0, max_digits=5, decimal_places=3, verbose_name='权重')
    #转移到result2里面
    #sum_result =encrypt(models.DecimalField(default=0, max_digits=4, decimal_places=1, verbose_name='实际得分'))

然后对实际得分进行汇总统计,全部加密的话,汇总统计 annotate,aggregate用不了,怎么办?

把需要汇总统计的字段,取消加密,通过one-to-one,关系表,移到其他表。

4、汇总统计:
SQL : select columnA , sum(columnB) group by columnA
(group by 非count 非sum的字段)

django: Model.Object.values('columnA').annotate(sum_1=Sum('columnB'))


参考 https://simpleisbetterthancomplex.com/tutorial/2016/12/06/how-to-create-group-by-queries.html

 
原文地址:https://www.cnblogs.com/lxgbky/p/14580734.html