Hi, I have this model
class Manifold_monitoring(models.Model):
MFD_type = models.ForeignKey(Manifo_types , on_delete=models.CASCADE)
DATE_TEST = models.DateField()
Pressure_MFD = models.DecimalField(max_digits=15, decimal_places=3,null=True, blank=True)
Pressure_SP = models.DecimalField(max_digits=15, decimal_places=3,null=True, blank=True)
.....
def __str__(self):
if self.MFD_type.Type and self.MFD_type.Fluid_N:
return str(self.MFD_type.MFDsID.MFDsID +' '+ self.MFD_type.Type +' '+ self.MFD_type.Fluid_N)
else:
return str(self.MFD_type.MFDsID.MFDsID +' '+ self.MFD_type.Type)
def get_absolute_url(self):
return reverse('WellNetwork')
class Meta:
ordering = ('-post_date',)
unique_together=[['MFD_type','DATE_TEST']]
everything works well when I add data from the Admin panel.
so when adding data twice it prevents me (unique_together
).
but using the views function it save the data and bypass the unique_together
condition?
I am using AJAX to send data from HTML page to the views function.
def AddManifold_Monto(request):
form = Manifold_monitoring_F()
model = Manifold_monitoring.objects.filter( DATE_TEST__exact= datetime.date.today())
if request.method == 'POST':
try:
MFD_type = request.POST.get('MFD_type')
DATE_TEST = request.POST.get('DATE_TEST')
Pressure_SP = request.POST.get('Pressure_SP')
Pressure_MFD = request.POST.get('Pressure_MFD')
manold_mntorng = Manifold_monitoring()
manold_mntorng.MFD_type_id = MFD_type
manold_mntorng.DATE_TEST = DATE_TEST
manold_mntorng.Pressure_SP = Pressure_SP
manold_mntorng.Pressure_MFD= Pressure_MFD
manold_mntorng.author = request.user
manold_mntorng.save()
messages.success(request,'You successfully added '+ str(manold_mntorng.MFD_type) +' to Manifolds List') # for python 3.5
return redirect('Add_ManifolMntr')
except:
messages.warning(request, 'Already exists, check input data!!!')
return redirect('Add_ManifolMntr')
context={
'title':'Manifold Monitoring', #+str(datetime.date.today),
'model':model,
'form': form,
}
return render(request,'Home/Production_Ser/MFD_Monitor/ADDMFD_Monitor.html',context)
So what is the solution or do I need to add a validators function to the model?