抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

需要只让用户查看自己的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.contrib.auth.models import User
关联User字段 = models.ForeignKey(User,on_delete=models.CASCADE,blank=True,null=True)


@admin.register()
class 类管理(ImportExportActionModelAdmin):
exclude = ('关联User字段',)
def has_change_permission(self, request, obj=None):
has_class_permission = super(类管理, self).has_change_permission(request, obj)
if not has_class_permission:
return False
if obj is not None and not request.user.is_superuser and request.user.id != obj.关联User字段.id:
return False
return True
def get_queryset(self, request):
if request.user.is_superuser:
return 类.objects.all()
return 类.objects.filter(关联User字段=request.user)
def save_model(self, request, obj, form, change):
if not change:
obj.关联User字段 = request.user
obj.save()

参考:http://loonlog.com/2020/11/19/django-user-admin-author/

评论