tengo una app en la que hago un CRUD de prestamo de libros, un libro solo puede ser prestado una vez(tiene una relacion onetoone con la tabla clientes), como podria hacer para chequear si el libro ya existe en la "loansTable" para que envie un mensaje al intentar agregarlo nuevamente (aca paso las tres tablas juntas pero estan cada una en una app diferente)
models.py
class loansTable(models.Model):
bookLoan = models.OneToOneField(booksTable,null=True,blank=True,on_delete=models.CASCADE)
clientLoan = models.ForeignKey(clientsTable,null=True,blank=True,on_delete=models.CASCADE)
dateLoan = models.DateField()
dateReturn = models.DateField()
status = models.CharField(max_length=10,default='borrowed')
def __str__(self):
return '{}'.format(self.bookLoan,self.clientLoan,self.status)
class booksTable(models.Model):
name = models.CharField(max_length=50)
author = models.CharField(max_length=50)
def __str__(self):
return '{}'.format(self.name)
class clientsTable(models.Model):
name = models.CharField(max_length=50)
adress = models.CharField(max_length=50)
phone = models.CharField(max_length=50)
email = models.EmailField()
def __str__(self):
return '{}'.format(self.name)
Views.py
def loans_list(request):
qs = loansTable.objects.all()
paginator = Paginator(qs,5)
page_number = request.GET.get('page',1)
try:
page_obj = paginator.page(page_number)
except (PageNotAnInteger, EmptyPage):
raise Http404(f'Invalid page {page_number}. That page contains no results')
context = {'list':qs, 'object_list': page_obj,'page_obj': page_obj}
return render(request, 'loansApp/loans.html',context)
def loan_add(request):
if request.method == 'POST':
form= LoansForm(request.POST)
if form.is_valid():
form.save()
return redirect('/loansApp/loans')
else:
form= LoansForm()
return render (request,'loansApp/addloan.html',{'form':form})