ysql> select * from exampleapp_author;
+------+-------------------------+---------------+
| id | auth_name | author_id |
+------+-------------------------+---------------+
| 1 | Sreenivasulu | NULL |
+------+-------------------------+---------------+
1 row in set (0.02 sec)
mysql> select * from exampleapp_book;
+------+-----------+
| id | name |
+------+-----------+
| 1 | sree225 |
+------+------------+
1 row in set (0.00 sec)
models.py
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=100, verbose_name='Book Name')
class Author(models.Model):
author = models.OneToOneField(Book, on_delete=models.CASCADE)
auth_name = models.CharField(max_length=100, verbose_name='Author Name')
How can i map two table ids using django views
from django.http import HttpResponse
from django.shortcuts import render
from .models import Author, Book
from .forms import AuthorForm, BookForm
def home(request):
if request.method == 'POST':
form = BookForm(request.POST)
aform = AuthorForm(request.POST)
if form.is_valid():
book = form.save()
aform.save(commit=False)
aform.book = book
aform.save()
return HttpResponse('sucess')
form = BookForm()
aform = AuthorForm()
return render(request, 'home.html', {
'form': form,
'aform': aform
})
template:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{{ aform.as_p }}
<input type="submit">
</form>