moj*_*bro 3 python django django-models model-inheritance
我想将事件存储在我正在讨论的Web应用程序中,我对每种方法的利弊都非常不确定 - 广泛使用继承或以更适度的方式使用继承.
例:
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()
Run Code Online (Sandbox Code Playgroud)
感觉就像我正在疯狂地创建数据库表.它需要很多连接来选择出来并且可能使查询复杂化.但我认为它的设计感觉很好.
使用只有更多字段的"更平坦"模型会更合理吗?
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)
Run Code Online (Sandbox Code Playgroud)
感谢您对此的评论,任何人.
菲利普
归档时间: |
|
查看次数: |
1760 次 |
最近记录: |