django admin 自动选择登录用户作为客服归属

后台为了屏蔽某些信息,比如客服时候只能看见自己,不能看到其他人,一旦选择了自己后,则无法再修改客服归属

class NotesAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'kefu':
            kwargs['queryset'] = User.objects.filter(username=request.user.username)
        return super(NotesAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    def get_readonly_fields(self, request, obj=None):
        if obj is not None:
            return self.readonly_fields + ('kefu',)
        return self.readonly_fields

    def add_view(self, request, form_url="", extra_context=None):
        data = request.GET.copy()
        data['kefu'] = request.user
        request.GET = data
        return super(NotesAdmin, self).add_view(request, form_url="", extra_context=extra_context)

 

posted @ 2023-03-30 16:17  super_ip  阅读(19)  评论(0编辑  收藏  举报