from forestadmin.datasource_toolkit.decorators.hook.context.delete import ( HookBeforeDeleteContext)defbefore_delete_hook(context: HookBeforeDeleteContext): is_allowed =True# Your logic hereif is_allowed isFalse: context.throw_forbidden_error("This collection is protected, you cannot remove from it." )agent.customize_collection('Book').add_hook('Before', 'Delete', before_delete_hook)
Add validation to fields
Define a new route into your urls.py file.
Add a new method to your controller.
import refrom django.http import HttpResponsefrom django_forest.resources.views.detail import DetailViewfrom django_forest.resources.views.list import ListViewfrom app.models import BookclassCompanyView(ListView):defpost(self,request,pk,*args,**kwargs): patch = self.get_body(request.body)["data"]["attributes"]if"name"in patch and re.search(r'^Forest', patch["name"]):return self.error_response("All company names should begin with 'Forest'." )returnsuper().post(request, pk, *args, **kwargs)classCompanyDetailView(DetailView):defput(self,request,pk): patch = self.get_body(request.body)["data"]["attributes"]if"name"in patch and re.search(r'^Forest', patch["name"]):return self.error_response("All company names should begin with 'Forest'." )returnsuper().put(request, pk)