I am trying to run another method after a form is saved:
The method inc_rec gets the id field created from the class based view once saved
- Once form is saved: I want to retrieve all records in the table Checklist
- Iterate through each of the rows and add the records to the All_Inspection table and updating the company id.
I am a bit stuck – as I have tried using signals, and I have tried to override, but somewhere there is a gap.
Help appreciated – Thanks in advance.
class ParishCreateView(LoginRequiredMixin, CreateView):
model = Company
fields = ['str_Company_Name', 'str_City','str_post_code']
#success_url = '/'
def form_valid(self, form):
print('form_valid')
form.instance.author = self.request.user
return super().form_valid(form)
def inc_rec(self):
Checklist = Checklist.objects.all()
id = self.request.id
for rec in Checklist:
new_rec=All_Inspection(str_status=rec.str_status, str_check=rec.str_check, str_comment='',company_id=id)
new_rec.save()
I am expecting new records to be added into ALL_Inspection table once the form is saved with the new created item.