After a lot of searching and only finding a few techniques that will allow me to do this (and even fewer with working examples), I bring it to you.
Following is a class structure similar to that with which I'm working:
# sources/models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=256)
slug = models.SlugField()
class Source(models.Model):
author = models.ForeignKey(Author)
url = models.URLField(help_text='The URL where a copy of the source can be found.')
class Book(Source):
title = models.CharField(max_length=256)
page = models.PositiveSmallIntegerField(help_text='Page where the source text appears.')
class MagazineArticle(Source):
magazine_name = models.CharField(max_length=256)
issue_date = models.DateField()
title = models.CharField(max_length=256)
And in a separate app, I would have this:
# excerpts/models.py
from django.db import models
from sources.models import Source
class Excerpt(models.Model):
excerpt = models.TextField()
source = models.ForeignKey(Source)
# Perhaps should be:
# source = models.OneToOneField(Source)
The catch being that in the admin, I want to be able to create either a Book
or a MagazineArticle
as the source for an excerpt without having separate fields in the excerpt for each.
One way I've read about doing this that might work is generic relations, possibly using an abstract base class instead, but I haven't found any examples that make sense in my context.
What are some methods of executing this (preferably with examples)?