morgancodes
morgancodes

Reputation: 25265

Django Model Inheritance -- prevent subclass instances from appearing in superclass admin

I have two classes:

class Article(ContentAsset):

    title =  models.CharField(max_length=2000)
    author =  models.CharField(max_length=2000, blank=True)
    source = models.CharField(max_length=2000, default="")
    datePublished = models.DateField(default=datetime.now)

    def __unicode__(self):
        return self.title

and

class PubMedArticle(Article):
    pubMedID = models.CharField(max_length=100)
    abstract = models.TextField(max_length=20000)

All of the PubMedArticle instances show up in the admin interface twice -- under the list of all PubMedArticle objects , and list of all Article objects. Is this normal behavior? I'd think that a subclass wouldn't normally show up in the admin list of its superclass. What's the best way to ensure that these PubMedArticle objects show up only under the PubMedArticle admin list?

Upvotes: 0

Views: 193

Answers (1)

singer
singer

Reputation: 2636

This is normal behavior. From database point of view table primary key of PubMedArticle just refers to Article table. So for each record in PubMedArticle table must be a record in Article table.

Now to the admin. The are two ways:

1) Make Article model abstarct - a good idea if you dont need unique primary keys to all of your articles. 2) Customize django admin list query.

Upvotes: 1

Related Questions