Model inheritance approach with Django ORM

I want to store events in a web application with which I am deceived, and I am completely not sure about the advantages and disadvantages of each appropriate approach - using inheritance broadly or more modestly.

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 type

class UserCancellationEvent(UserEvent):
    reason = models.CharField()

It seems to me that I am creating database tables such as crazy. This will require a large number of associations to choose things and may make it difficult to request. But the design seems nice, I think.

Would it be wiser 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

+3
source share
2 answers

, . , " " - : , .

+6

, . , , , . .

+1

Source: https://habr.com/ru/post/1716944/


All Articles