title: save data to database using django
body: <p><strong>views.py</strong> </p>
  
  <pre><code>def addbook(request): 
      if request.POST:
          book_form = BookForm(request.POST)
      author_form = AuthorForm(request.POST)
          if (book_form.is_valid() and author_form.is_valid()):
          log.debug("test....")
              book=book_form.save()
              author=author_form.save()
          author.book=book
          author.save()
  
          return redirect('/index/')
      else:
      book_form=BookForm()
          author_form=AuthorForm()
          return render_to_response('addbook.html',{'form':book_form,'form':author_form},context_instance=RequestContext(request))
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>from django import forms
  from django.forms import ModelForm
  from myapp.models import Book,Author
  
  
  class AuthorForm(ModelForm):
  
      class Meta:
          model = Author
  
          fields=['author_id','first_name','last_name','email','age']        
  
  
  class BookForm(ModelForm):
  
      class Meta:
          model = Book
  
          fields=['book_id','book_name','publisher_name','author']        
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.db import models
  
  class Author(models.Model):
      author_id = models.AutoField(primary_key=True)
      first_name = models.CharField(max_length=30)
      last_name = models.CharField(max_length=40)
      email = models.EmailField()
      age=models.IntegerField()
  
      class Meta:
      db_table=u'Author'
  
      def __unicode__(self):
          return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)
  
  
  class Book(models.Model):
      book_id=models.AutoField(primary_key=True,unique=True)
      book_name=models.CharField(max_length=30)
      publisher_name=models.CharField(max_length=40)
      author=models.ForeignKey(Author)
  
      class Meta:
          db_table = u'Book'
  
      def __unicode__(self):
          return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)
  </code></pre>
  
  <p><strong>templates.html</strong></p>
  
  <pre><code>&lt;div align="center"&gt;
  
    &lt;form action="/addbook/" method="post"&gt;
       {% csrf_token %}
      {{ book_form.as_p }}
      {{ author_form.as_p }}
      &lt;input type="submit" value="submit"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>Here I am using author as foriegnkey.In form,I had given fields for all the fields in database.When I click the submit button it is showing the fields,along with author field. Here author_id in author table is pointing the author_id in book table.In views.py how we can implement that.</p>
  
  <p>I am expecting to save the data into database in this way.I changed my template also.</p>