我正在使用 DjangoDeleteView删除我的数据库中的项目。我使用单独的模板来显示删除确认消息,但是当我按下是按钮时,我得到了ProtectedError因为客户表与帐户表相关联。因此,我想ProtectedError在同一个模板中处理并为用户提供另一条消息。
这是我用来执行删除的代码:
class Customer(DeleteView):
#Delete Customers
model = Customer
template_name = 'project_templates/delete_customer.html'
def get_success_url(self):
return reverse('inactive_customers')
Run Code Online (Sandbox Code Playgroud)
如果有人可以建议我处理这种情况的方法,那就太好了。
django django-forms django-generic-views django-class-based-views
在下面,我有一个Post模型.一个Post对象有一个status可以是'unpublished'或的字段'published'.
if status is 'published',我想阻止对象被删除,并希望将此逻辑封装在模型本身中.
from model_utils import Choices # from Django-Model-Utils
from model_utils.fields import StatusField
class Post(model.Models)
STATUS = Choices(
('unpublished', _('Unpublished')),
('published', _('Published')),
)
...
status = StatusField(default=STATUS.unpublished)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?delete如果使用a批量删除对象,则覆盖该方法将不起作用QuerySet.我读过不使用接收器,但我不确定为什么.