Django newbie here, I created a simple form following this tutorial, and my form correctly saves the data in my Postgres connected local database. I was wondering, how can I trigger a function, whenever a valid form is saved into the database? The code I want to run is a simple function which is written in a python file, and it does some processing on the latest data given by the first form. I want it to run only when a valid form data is saved and was wondering if django signal trigger is my way to go. Feel free to ask for any further clarification. In other words, I want to do some post-processing on data, which is present inside the database, which is being filled by the form, and trigger the post-processing only when valid data is entered in the database.
Here is my code :
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import auditform, ClientAuditForm
from django.db.models.signals import post_save
from . import rocode
# def auditingfun(request):
# return HttpResponse('Auditing form works')
# # Create your views here.
def auditingfun(request):
if request.method == 'POST':
forminput = auditform(request.POST)
if forminput.is_valid():
Name = forminput.cleaned_data['Name']
Origin = forminput.cleaned_data['Origin']
ClientAddress = forminput.cleaned_data['ClientAddress']
DispatchType = forminput.cleaned_data['DispatchType']
ETA = forminput.cleaned_data['ETA']
GSTIN = forminput.cleaned_data['GSTIN']
# print(GSTIN,Name,Origin,Destination,MaterialType,Preference,ClientAddress,DispatchType,ETA)
forminput = auditform(request.POST)
return render(request, 'auditing/auditform.html', {'forminput': forminput} )
forms.py
from django import forms
from .models import auditModel
class auditform(forms.Form):
Origin = forms.CharField()
Destination = forms.CharField()
MaterialType = forms.CharField()
Preference = forms.CharField()
ClientAddress = forms.CharField(widget=forms.Textarea)
Name = forms.CharField()
GSTIN = forms.IntegerField()
DispatchType = forms.ChoiceField(choices=[('Question','Inbound'),('Other','Outbound')])
ETA = forms.CharField()
class ClientAuditForm(forms.ModelForm):
class Meta:
model = auditModel
fields = ('Origin','Destination','MaterialType','GSTIN','Name','Preference','ClientAddress','DispatchType','ETA')
Just for simplicity, imagine the customcode (imported in the views.py file as rocode.py) I have just adds the data entered and stores the data in the same database, in a different column.