2013-03-04 68 views
0

models.pyDjango的模型,views.py和模板外鍵分配

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 Info' 

    def __unicode__(self): 
     return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age) 

    def books(self): 
     return Book.objects.filter(author=self) 

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 Name' 

    def __unicode__(self): 
     return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name) 

views.py

def addbook(request): 
    if request.POST: 
     first_name = request.POST.get('first_name') 
     last_name = request.POST.get('last_name') 
     email = request.POST.get('email') 
     age = request.POST.get('age') 
    author = Author(first_name = first_name,last_name = last_name,email=email,age=age) 
     author=author.save() 
     book_name = request.POST.get('book_name') 
     publisher_name = request.POST.get('publisher_name') 
    #Book.author_id=author 
     #author_id = author_id() 
     book=Book.objects.create(book_name=book_name,publisher_name=publisher_name,author_id) 
    book.save() 
    return redirect('/index/') 
    else: 
     return render_to_response('addbook.html',context_instance=RequestContext(request)) 

的index.html /模板

table border="0" cellpadding='8' cellspacing='10'> 
    <tr> 
     <td align="right" colspan="8"><a href="/addbook/">Add Book</a></td> 
    </tr> 

    <tr> 
     <th>Book Id</> 
    <th>Book name</th> 
    <th>Publication name</th> 
    <th>Author Id</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>E Mail</th> 
    <th>Age</th> 
    </tr> 
    {% for book in books %} 
    <tr> 
     <td>{{ book.book_id }}</td> 
     <td>{{ book.book_name }}</td> 
     <td>{{ book.publisher_name }}</td> 
     <td>{{ book.author_id }}</td> 

     {% for author in authors %} 
     <td>{{author.first_name}} </td><td>{{author.last_name}}</td> 
     <td>{{author.email}}</td> 
     <td>{{author.age}}</td> 
      {% endfor %} 
      <td><a href="/editbook/{{ book.book_id}}">Edit</a></td> 
      <td><a href="/deletebook/{{ book.book_id}}">Delete</a></td> 
     {% endfor %} 

這裏怎麼要在Book類中爲「作者」或author_id字段分配外鍵值,請給出賦值過程。我無法將值賦給forreign key fi現在...如何分配或如果有任何其他功能可用,請解釋我

+2

1.修復您的壓痕。 2.修正你的語言:英語可能不是你的第一語言,但沒有理由使用「PLZ」而不是「請」等。3.「不工作」沒有幫助。怎麼了?你會得到什麼錯誤?發佈追蹤。 4.請發郵件給我一個問題。 – 2013-03-04 09:39:01

+0

先生,我無法通過網頁將數據插入數據庫,錯誤是「IntegrityError at/addbook /」,請幫我先生,如果您覺得這段代碼不正確,您可以給我一個正確的代碼,以便學習相同的代碼。 – user2086641 2013-03-04 09:45:52

回答

1

問題是這樣的:

author = Author(first_name = first_name,last_name = last_name,email=email,age=age) 
author=author.save() 

save()實際上並沒有返回任何東西,即使我相信它應該。將其更改爲簡單:

author = Author(first_name = first_name,last_name = last_name,email=email,age=age) 
author.save() 
book.author = author 
book.save() 

現在你有你的作者,你可以分配給book.author

此外,雖然我在這裏,您應該瞭解有關reverse relations。不需要下面的方法:

def books(self): 
    return Book.objects.filter(author=self) 

因爲你可以這樣做,而不是:

author.book_set.all() 
+0

它的工作..感謝 – user2086641 2013-03-04 16:30:50

+0

@ user2086641那麼你應該接受這個答案(並upvote它),如果它幫助你。 – 2013-03-04 20:22:22

1

完整性錯誤是由於空值傳遞給book表的author_id列。檢查此行book=Book.objects.create(book_name=book_name,publisher_name=publisher_name,author_id)

如果妳想要插入同一作者的ID成冊表,或者您可以使用

Django的信號

,或者你必須從獲取最新的作者ID通過與一些獨特的參數(比如值電子郵件ID)查詢作者表,並通過這個ID來Books表

+0

你能告訴我代碼從作者表中獲取數據的一些參數 – user2086641 2013-03-04 10:13:11