I have two models, a Author and a Article. Each Article is needs to refer back to its Author so I can access its values in my template. What is the most efficient way of doing this?
class Author(models.Model):
name = models.CharField(max_length=256)
picture = models.CharField(max_length=256)
class Article(models.Model):
title = models.CharField(max_length=256)
author = #The Author model that wrote this article
date = models.DateTimeField(default=datetime.now)
body = models.TextField()
You need to use the
Foreign Key
concept for it. The following is its implementation:While saving it, you need to do the following in your
views.py
:Hope it helps...