Django admin 常用方法 model 增加只读权限

时间:2021-07-20 10:14:22

1.Django admin model 设置查看权限

Django model 默认只有增加、删除、修改权限。没有查看权限

#model
class Ad_Campaing(models.Model):
TEST_CHANNEL = (
('adx-1', '渠道1'),
('adx-2', '渠道2'),
('adx-3', '渠道3'),
('adx-4', '渠道4'),
('adx-5', '渠道5'),
('adx-no', '不测试') )
AD_CAMPAING_STATUS = (
('dsh', '待审核'),
('tfz', '投放中'),
('zt', '暂停'),
('ygq', '已过期'),
('yebz', '余额不足'),
('bgdb', '曝光达标'),
('djdb', '点击达标'),
('ready', '未到开始时间'),
('ysdb', '预算达标')
) name = models.CharField(max_length=200, verbose_name="广告组名称", default="")
status = models.CharField(max_length=20, verbose_name='广告组状态', default="dsh",
choices=AD_CAMPAING_STATUS)
advertiser = models.ForeignKey(UserProfile, verbose_name='广告主ID', null=True, blank=True)
start_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
#
budget = models.IntegerField(verbose_name='计划预算', null=False, blank=False, default=0)
# pay
max_impression = models.IntegerField(verbose_name='最大曝光数', null=True, blank=True)
max_click = models.IntegerField(verbose_name='最大点击数', null=True, blank=True)
pay_money = models.IntegerField(verbose_name='消费金额', null=True, blank=True)
# for free
free_max_impression = models.IntegerField(verbose_name='赠送曝光数', null=False, blank=False, default=0)
free_max_click = models.IntegerField(verbose_name='赠送点击数', null=False, blank=False, default=0) # 不要物理性删除数据, 如果要删除某条数据,就将enable置为0
enable = models.IntegerField(verbose_name='是否有效', null=False, blank=False, default=1)
unit_price = models.FloatField(verbose_name='单价', default=0.5)
weight = models.IntegerField(verbose_name='权重', default=50)
permit_min_vername = models.CharField(max_length=20, verbose_name="最小app版本号", default="4.6.7")
action = models.IntegerField(verbose_name='跳转类型', default=1)
permit_connect_type = models.CharField(max_length=20, verbose_name="允许跳转类型", default="[1, 2, 3, 4, 5]")
test_channel = models.CharField(max_length=20, verbose_name='测试渠道', default="adx-1",
choices=TEST_CHANNEL)
finish_percent = models.IntegerField(verbose_name='任务完成率', default=0)
add_time = models.DateTimeField(auto_now=True) class Meta:
verbose_name = "广告计划"
verbose_name_plural = verbose_name
permissions = (
('view_ad_campaing', 'view_ad_campaing'),
)

Django admin 常用方法 model 增加只读权限

admin 文件

class Ad_CampaingAdmin(admin.ModelAdmin):
"""
广告计划
""" list_display = ['id', 'name', 'status', 'advertiser', 'weight', 'operate', 'unit_price', 'budget', 'start_time',
'end_time'] # 后台自定义显示 search_fields = ['id', 'name', 'status', 'advertiser'] # 定义后台搜索 #list_filter = ['id', 'name', 'status', 'advertiser'] # 通过时间搜索 #自定义表格显示字段
def operate(self, obj): sum_weight = Ad_Campaing.objects.filter(status='tfz').aggregate(Sum('weight'))
sum_weight = sum_weight.get('weight__sum')
print(sum_weight)
if obj.status == 'tfz':
try:
per_weight = round(float(obj.weight) / float(sum_weight) , 2)
except Exception as e:
per_weight = 0
else:
per_weight = 0
html = '{}'.format(per_weight)
return html operate.allow_tags = True
operate.short_description = '投放比例' def get_readonly_fields(self, request, obj=None):
"""
设置只读字段
:param request:
:param obj:
:return:
"""
# black5.view_ad_campaing black5 为app名字
if not request.user.is_superuser and request.user.has_perm('black5.view_ad_campaing'):
return [f.name for f in self.model._meta.fields]