2011-09-02 36 views
0

如果Django世界中有人可以教授關於現場查找的最佳實踐以及它們如何工作。我試過他們是文檔,但我發現它非常混淆模型和關於數據庫的觀點之間的關係。ForeignKey關係更好的理解

例如,假設我們具有以下結構: 視圖應該如何顯示以及處理這種情況的最佳方式是什麼。

機型:

class Article(models.Model): 

    Author = models.CharField(max_length=255) 
    Title = models.CharField(max_length=255) 
    Content = models.TextField() 



class comments(models.Model): 

    TheArticle = models.ForeignKey(Article) 
    c_Author = models.CharField(max_length=255) 
    c_Title = models.CharField(max_length=255) 
    c_Content = models.TextField() 

查看:

def page(request , URL_Title): #comes trough the url setup 
    article = Article.objects.get(Title=URL_Title) 
    comments = # What is the Django way in getting the comments for the specified article ? 
    return render_to_response('base.html' , {'article':article , 'comments':comments }) 

的Themplate:

{% if article %} 
.....Print all the article related fields ..... 
    {% if comments %} 
     {% for comment in comments %} 
     .....Print Comment ..... 
     {% endfor %} 
    {% endif %} 
{% endif %} 

如果你有時間上的多對多關係的例子也會有幫助。
任何幫助,高度讚賞。

回答