I am creating a form which is able to create a post if not existed or update the post if already existed. I want to do it using the same url and view. The django generic class view uses two urls for this. Although I was able to create a post but for updating, I can't fetch the existing data of corresponding foreign key. I would like to update those existing data if some data are already existed inside the database. My code are as follows.
models.py
class BookDetail(models.Model):
book = models.ForeignKey(Book,default=None,on_delete=models.CASCADE)
book_summary = models.CharField(max_length=255,null=True)
book_details = RichTextUploadingField(blank=True,null=True)
timestamps = models.DateTimeField(auto_now_add=True,auto_now=False)
updated = models.DateTimeField(auto_now_add=False,auto_now=True)
The bookdetails consists of foreign key for book model. I want to create book details using those foreign key. But if the details are already created and the user wants to update those detail, I want it to be updated using same url. I had used update_or_create method of django. But I wasn't able to send form data in the defaults attribute of update_or_create method. How can I create and update those data using same url?