Django form validation when creating new records

Hello,

I’m using Forest Admin on a Django project which has a heavily customised admin panel. I’m trying to figure out how to implement these customisations in Forest Admin instead, especially the validation stage between creating a new record in Forest Admin and saving it in the DB.

For example, I want to be perform additional actions once I have clicked “create”.

I would appreciate it if someone could point me in the right direction here. I’ve looked at the Developer Guide, but mostly it’s related to smart fields and so on, which is great, but not what I’m searching for this particular issue.

Thank you very much!

Hello @Kimo :wave:

There are several ways to directly bring your customisations to your Forest agent:.
Indeed there are Smart Fields but there are also Smart Actions.

If you have the Forest routes/ folder, you can potentially tweak the route that handles the “create” following this part of our documentation.

Tell me if that helps :muscle:
Cheers :pray:

1 Like

Thank you very much for your reply, Romain! This is indeed helpful.

I have another question, if I may! :smiley:

I’m trying to use this example, to protect certain records:

from django.http import HttpResponse

from django_forest.resources.utils.resource.views import DetailView


class CompaniesDetailView(DetailView):

    def delete(self, request, pk):
        if (pk == 82):
            return HttpResponse(
            'This record is protected, you cannot remove it.',
             status=403);

        return super(CompaniesDetailView, self).delete(request, pk)

My questions are:

  1. If I have an app where there are several models, do I need to declare the specific model I’m targeting somewhere in the above code?
  2. Do I have to include this view in the urls.py file which I created inside the app/forest/ directory?

Thank you,
Kimo

Hi @Kimo,

You should create an urls.py file as explained here.

If you need to protect several models, you need to override all the views and add them in the urls.py.
ex:
/companies
/companies/count
/customers/
/customers/count/