Model inheritance approach with Django's ORM
django, django-models, model-inheritance, python
Solution
Flat is better than nested. I don't see that the "deep inheritance" is really buying you anything in this case: I'd go for the flatter model as a simpler, plainer design, with likely better performance characteristics and ease of access.
Problem
I want to store events in a web application I am fooling around with and I feel quite unsure about the pros and cons of each respective approach - using inheritance extensively or in a more modest manner. Example: ``` class Event(models.Model): moment = models.DateTimeField() class UserEvent(Event): user = models.ForeignKey(User) class Meta: abstract = True class UserRegistrationEvent(UserEvent): pass # Nothing to add really, the name of the class indicates it's type class UserCancellationEvent(UserEvent): reason = models.CharField() ``` It feels like I'm creating database tables like crazy. It would require alot of joins to select things out and might complicate querying. But it's design feels nice, I think. Would it be more reasonable to use a "flatter" model that just has more fields? ``` class Event(models.Model): moment = models.DateTimeField() user = models.ForeignKey(User, blank=True, null=True) type = models.CharField() # 'Registration', 'Cancellation' ... reason = models.CharField(blank=True, null=True) ``` Thanks for your comments on this, anyone. Philip