I want to create a Category class that may or may not have a subcategory or it may or may not itself be a subclass of another Category object.
This doesn’t work, but it gives an idea of what I’m trying to do:
class Category(models.Model):
about = models.TextField(blank=True)
parent_cat = models.ForeignKey(Category, blank=False, null=True)
slug = models.SlugField(unique=True, help_text = SLUG_HELP)
title = models.CharField(max_length = 26, unique=True)
…
What works right now is I have a SubCategory class that can relate to a Category. However I would like to make the code more flexible by just allow Categories to be sub-categories of themselves. Then I can have an unlimited number of parents/children. Can someone suggest how I might be able to do this?
Your reference to subclassing and inheritance is confusing. You’ve got a standard recursive relationship, which works fine via a ForeignKey. The only thing you’d need to do would be to define the FK as described in the documentation:
What else “doesn’t work” about the code you have posted?