使用 django-rules 时 Django Admin 中谓词的 AttributeError
AttributeError from predicate in Django Admin when using django-rules
我正在尝试使用 django-rules 创建第一个实现,遵循 README 中的指南。
从一些基本概念开始,我想在我的应用程序中将记录删除限制为记录所有者 contact
。我设法(似乎)让事情与我的 API 一起工作,通过 Django REST Framework 提供。但是,当我用非超级用户打开 Django Admin 时,出现以下错误:
AttributeError: 'NoneType' object has no attribute 'created_by'
这似乎与我在 contact/rules.py
中的谓词定义有关(我尝试遵循此处 django-rules 文档中的 "book example"):
import rules
@rules.predicate
def is_contact_owner(user, contact):
return contact.created_by == user
@rules.predicate
def is_address_owner(user, address):
return address.created_by == user
rules.add_perm("contact.view_contact", rules.is_staff)
rules.add_perm("contact.add_contact", rules.is_staff)
rules.add_perm("contact.change_contact", rules.is_staff)
rules.add_perm("contact.delete_contact", is_contact_owner)
rules.add_perm("contact.view_address", rules.is_staff)
rules.add_perm("contact.add_address", rules.is_staff)
rules.add_perm("contact.change_address", rules.is_staff)
rules.add_perm("contact.delete_address", is_address_owner)
我的contact/admin.py
看起来如下:
from django.contrib import admin
from rules.contrib.admin import ObjectPermissionsModelAdmin
# Register your models here.
from .models import Address, Contact
@admin.register(Address)
class AddressAdmin(ObjectPermissionsModelAdmin):
# class AddressAdmin(admin.ModelAdmin):
list_display = (
"id",
"local_address",
"country",
"uuid",
"created",
"modified",
"created_by",
"modified_by",
)
date_hierarchy = "created"
list_filter = ["created", "modified", "country"]
@admin.register(Contact)
class ContactAdmin(ObjectPermissionsModelAdmin):
# class ContactAdmin(admin.ModelAdmin):
list_display = (
"id",
"last_name",
"first_name",
"date_of_birth",
"uuid",
"created",
"modified",
"created_by",
"modified_by",
)
date_hierarchy = "created"
list_filter = ["created", "modified"]
任何人都可以指点一下这里可能失败的原因吗?
所以在添加一些日志记录之后,我发现我的谓词函数似乎在每个请求上都被执行,即使只应检查查看权限也是如此。在管理员登录页面上观察到错误,其中没有我想要检查的对象 created_by
属性 在上下文中。
这引出了另一个问题:为什么每次请求都要检查谓词? -> 新 post
我正在尝试使用 django-rules 创建第一个实现,遵循 README 中的指南。
从一些基本概念开始,我想在我的应用程序中将记录删除限制为记录所有者 contact
。我设法(似乎)让事情与我的 API 一起工作,通过 Django REST Framework 提供。但是,当我用非超级用户打开 Django Admin 时,出现以下错误:
AttributeError: 'NoneType' object has no attribute 'created_by'
这似乎与我在 contact/rules.py
中的谓词定义有关(我尝试遵循此处 django-rules 文档中的 "book example"):
import rules
@rules.predicate
def is_contact_owner(user, contact):
return contact.created_by == user
@rules.predicate
def is_address_owner(user, address):
return address.created_by == user
rules.add_perm("contact.view_contact", rules.is_staff)
rules.add_perm("contact.add_contact", rules.is_staff)
rules.add_perm("contact.change_contact", rules.is_staff)
rules.add_perm("contact.delete_contact", is_contact_owner)
rules.add_perm("contact.view_address", rules.is_staff)
rules.add_perm("contact.add_address", rules.is_staff)
rules.add_perm("contact.change_address", rules.is_staff)
rules.add_perm("contact.delete_address", is_address_owner)
我的contact/admin.py
看起来如下:
from django.contrib import admin
from rules.contrib.admin import ObjectPermissionsModelAdmin
# Register your models here.
from .models import Address, Contact
@admin.register(Address)
class AddressAdmin(ObjectPermissionsModelAdmin):
# class AddressAdmin(admin.ModelAdmin):
list_display = (
"id",
"local_address",
"country",
"uuid",
"created",
"modified",
"created_by",
"modified_by",
)
date_hierarchy = "created"
list_filter = ["created", "modified", "country"]
@admin.register(Contact)
class ContactAdmin(ObjectPermissionsModelAdmin):
# class ContactAdmin(admin.ModelAdmin):
list_display = (
"id",
"last_name",
"first_name",
"date_of_birth",
"uuid",
"created",
"modified",
"created_by",
"modified_by",
)
date_hierarchy = "created"
list_filter = ["created", "modified"]
任何人都可以指点一下这里可能失败的原因吗?
所以在添加一些日志记录之后,我发现我的谓词函数似乎在每个请求上都被执行,即使只应检查查看权限也是如此。在管理员登录页面上观察到错误,其中没有我想要检查的对象 created_by
属性 在上下文中。
这引出了另一个问题:为什么每次请求都要检查谓词? -> 新 post