I have a model where a user can add some data and assign this data to a specific month. But if that user had already added some items and try to delete a specific data who is assigned to a previous month it should be locked.
I had success with this, but my problem now is with the message for the user. I would like to remove the default message of the delete button on admin but until now without success.
Here is the image of the problem: MessageImage
admin.py
class MontlyReleaseAdmin(admin.ModelAdmin):
form = MontlyReleaseForm
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def delete_model(self, request, obj):
if not is_valid_date(obj):
messages.add_message(request, messages.ERROR, 'Not Deleted')
else:
super().delete_model(request, obj)
admin.site.register(MontlyRelease, MontlyReleaseAdmin)
You can override the has_delete_permission(…)
method [Django-doc] to prevent deletion:
class MontlyReleaseAdmin(admin.ModelAdmin):
# …
def (self, request, obj=None):
if not is_valid_date(obj):
messages.add_message(request, messages.ERROR, 'Not Deleted')
return False
return super().has_delete_permission(request, obj)
here you thus do not override the delete_model(…)
method itself, but the method that checks if you can delete the object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.