Using Django 2.0 I have a model class named Book
that asks for title
, author
, year
but I have another column in that row that requires for a unique id based on the authors name, book title, and random numbers. This is my model
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=50)
year = models.PositiveSmallIntegerField()
identification = models.CharField(max_length=50, blank=True)
holder = models.ForeignKey('Person', related_name='book_holder', on_delete=models.PROTECT(), null=True, blank=True)
available = models.BooleanField(default=True, blank=True)
overdue = models.BooleanField(default=False, blank=True)
and it will be used in a form that only asks for the title
, author
, and year
but what im looking for is how can i fill the identification
column after the user submits the form. So for example if the user enters Harry Potter
for the title
and J. K. Rowling
for the author
, I want identification
to have a value of a combination of both plus an eight digit random number at the end something like JH28194637
but I also need this column to be unique for every book so if i had to of the same Harry Potter books, I would need both to be different so one could be JH28194637
while the other one could be JH39287104
. I will be using ajax to submit my form and I was thinking of maybe having a variable in my javascript that builds my identification
when the user fills in the author
and title
fields and then passing it to my ajax call. Would that be the best way? But then how would I be able to check if the variable value already exists in the database or what would be the best way to do this task.