i have 2 model one is Tempdriver and other one is Hiring, i am register new customer in Tempdriver table, i need when i edit tempdriver record and status is (accept) and when i save this record then need to same record create on hiring table with matching column with status=(Applied on app) rest column should be null in hiring table
models.py
class Tempdriver(BaseModel): name = models.CharField(max_length=255) mobile = models.CharField(max_length=20,unique=True,null=True, blank=True) alternate_number = models.CharField(max_length=20, null=True, blank=True) date = models.DateField(null=True, blank=True) address = models.CharField(max_length=200, null=True, blank=True) status = EnumField(choices=['Accept','Reject','Hold'], null=True) class Hiring(BaseModel): STATUS_CHOICES = (('', 'Type...'), ('HR Interview Pass', 'HR Interview Pass'), ('HR Interview Fail', 'HR Interview Fail'), ('Allocation Completed', 'Allocation Completed'), ('Applied on app','Applied on app') ) name = models.CharField(max_length=254, null=True, blank=True) mobile = models.CharField(max_length=20,unique=True,null=True, blank=True) city = models.ForeignKey(City, models.CASCADE, verbose_name='City', null=True, blank=True) age = models.PositiveIntegerField(null=True, blank=True) status = models.CharField(max_length = 255,choices=STATUS_CHOICES, null=True, blank=True)
forms.py
class TempDriverForm(forms.ModelForm): class Meta: model= Tempdriver fields='__all__'
views.py
def edit_temp_driver(request,id=0): tempdr=Tempdriver.objects.get(pk=id) form= TempDriverForm(request.POST or None, request.FILES or None, instance=tempdr) if form.is_valid(): edit = form.save(commit=False) edit.save() messages.success(request,'Driver data updated successfully!') return redirect('/fleet/tempdr') return render(request, 'hiringprocess/edit_tempdriver.html', {'form':form})
Advertisement
Answer
First, let me to ask, why you use BaseModel
instead of `models.Model’ ?(just for my information, i just wanted to ask in a comment, but i’m not permmited)
If i understand your question correctly, your function edit_temp_driver
would be something like this:
def create_hiring_object(form): try: obj = Hiring() obj.name = form.cleaned_data['name'] obj.mobile = form.cleaned_data['mobile'] obj.status = obj.STATUS_CHOICES[4] obj.save() except Exception as err: # return error return err def edit_temp_driver(request,id=0): tempdr=Tempdriver.objects.get(pk=id) form= TempDriverForm(request.POST or None, request.FILES or None, instance=tempdr) if form.is_valid(): edit = form.save(commit=False) # call to create hiring create_hiring_object(form) edit.save() messages.success(request,'Driver data updated successfully!') return redirect('/fleet/tempdr') return render(request, 'hiringprocess/edit_tempdriver.html', {'form':form})