2014-03-03 24 views
2

我有以下代碼:只顯示在Django中傳遞條件的ForeignKeys。

from django.db import models 

# Create your models here. 

class Book(models.Model): 
    title = models.CharField(max_length=200) 
    is_finished = models.BooleanField("Is this book finished?") 
    description = models.TextField("What's your book about? Think of this as the back cover") 
    def __unicode__(self): 
     return self.title 
    #book_cover = should take an img as input 


class Place(models.Model): 
    book = models.ForeignKey(Book) 
    name = models.CharField(max_length=200) 
    description = models.TextField() 
    def __unicode__(self): 
     return self.name 


class Chapter(models.Model): 
    book = models.ForeignKey(Book) 
    name = models.CharField(max_length=200) 
    text = models.TextField() 
    place = models.ForeignKey(Place) 
    def __unicode__(self): 
     return self.name 

最後一類是給我的麻煩。只有當該地點與我的章節相同時,我才能從地點選擇。

我該如何做到這一點?我對Django和編程一般都很新,很抱歉,如果我的問題很容易回答。我試過通過Django文檔搜索,但似乎沒有任何工作。

+0

你最初有什麼?書或章節或地點? – ndpu

+0

你在問什麼?如果我理解正確:我首先​​應該能夠創建一本書(帶有標題,描述和布爾is_finished)。這很好。然後,Place定義了所有可能發生書籍的不同位置(例如:Minessota,紐約和日本)。本章基本上是文本+它屬於哪本書+它發生的地方。 – joaquinlpereyra

+0

因此,如果您需要書中所有地方的列表:'place_list = Place.objects.filter(book = book_that_youve_created)' – ndpu

回答

0

模型Chapterbook場可以從place領域中扣除,所以你應該放棄它,然後由place.book訪問它。