姜戈&定制遗留数据库
我目前正在开发一个项目,为现有日历应用程序实现 Django 接口。日历应用程序使用MySQL作为后端数据库。
在我们的自定义应用程序中,我们希望修改/扩展现有日历应用程序使用的表之一中的数据,例如,
# Auto-generated by inspectdb - table used by calendar application
class CalendarEvent(models.Model:)
name = models.CharField(max_length=80)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
# Manually created table
class CustomCalendarEvent(models.Model:)
code = models.CharField(max_length=80) # Mapped from name
length = models.DateTimeField() # start_time - stop_time
.... additional data ....
我们还希望我们的数据表示与现有日历表保持同步,即当在日历应用程序这些将自动传播到我们的自定义表。
我可以想到一些明显的方法来做到这一点(例如,由 cron 启动的同步脚本或可能是 MySQL 触发器),但我不认为这些解决方案特别优雅。
一种可能性是为 CustomCalendarEvent 使用 自定义管理器并覆盖get_query_set功能来触发同步功能。
这是 Django CustomManager 的合法使用吗?如果没有,有人可以推荐解决此问题的替代方法吗?
I'm currently working on a project to implement a Django interface to an existing calendar application. The calendar application has MySQL as the backend DB.
In our custom application we would like to modify/extend the data in one of the tables used by the existing calendar application e.g.
# Auto-generated by inspectdb - table used by calendar application
class CalendarEvent(models.Model:)
name = models.CharField(max_length=80)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
# Manually created table
class CustomCalendarEvent(models.Model:)
code = models.CharField(max_length=80) # Mapped from name
length = models.DateTimeField() # start_time - stop_time
.... additional data ....
We would also like our representation of the data to remain in sync with the existing calendar table i.e. when new entries are made in the calendar application these would automatically propagate to our custom table.
I can think of a couple of obvious ways in which to do this (e.g. a synchronisation script initiated by cron or maybe MySQL triggers) but I don't feel that these solutions are particular elegant.
One possibility is to use a Custom Manager for the CustomCalendarEvent and override the get_query_set functionality to also trigger a synchronisation function.
Is this a legitimate use of Django CustomManagers? If not can anybody recommend an alternative approach to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在尝试使用更多字段来扩展 CalendarEvent。
首先,我将对 CustomCalendarEvent 进行以下更改:
code = models.CharField(max_length=80) # Mapped from name
calendar_event = models.ForeignKey(CalendarEvent)< /code>
如果 length 只是计算 start_time 和 end_time 之间的天数差异,我会将其从 CustomCalendarEvent 中删除,并使其在 CalendarEvent 中可调用(只是执行计算的方法)。
您确实不想在两个表之间复制数据 - 这就是当 CalendarEvent 中有
name
和 CustomCalendarEvent 中有code
时所得到的结果。对名称的更新必须同步到code
,如果您只想使用更多字段扩展 CalendarEvent 表,则没有理由这样做。然后 您可以覆盖
save()
和delete( )
方法 供 CalendarEvent 传播插入/删除更改。我相信,更新在您的情况下并不重要,因为 CustomCalendarEvent 只是 CalendarEvent 的扩展。替代方法:在 CalendarEvent 上使用数据库插入触发器,将条目传播到 CustomCalendarEvent。我仍然会让 CustomCalendarEvent 表在 CalendarEvent 中有一个外键,而不是复制数据。
编辑:顺便说一句,我永远不会使用 自定义管理器像您建议的那样修改数据,即使是某些读取操作的副作用。管理者的职责是查询,而不是修改数据。
It appears that you are trying to extend CalendarEvent with more fields.
First, I would make this change to CustomCalendarEvent:
code = models.CharField(max_length=80) # Mapped from name
calendar_event = models.ForeignKey(CalendarEvent)
and if length is just calculating the difference in days between start_time and end_time I would remove it from CustomCalendarEvent and make it a callable in CalendarEvent instead (just a method that does the computation).
You really don't want to be duplicating data between the two tables - which is what you get when you have
name
in CalendarEvent andcode
in CustomCalendarEvent. Updates to name will have to be synchronized tocode
and there is no reason for that if all that you want to do is extend the CalendarEvent table with more fields.Then you could override the
save()
anddelete()
methods for CalendarEvent to propagate the inserts/deletes change. Updates, I believe, do not matter in your case as CustomCalendarEvent is just an extension of CalendarEvent.Alternative approach: use a database insert trigger on CalendarEvent that propagates the entry to CustomCalendarEvent. I would still have the CustomCalendarEvent table have a foreign key into CalendarEvent instead of duplicating the data.
EDIT: By the way, I would never use a custom manager to modify data like you are suggesting, even as a side effect of some read operation. Managers are about querying, not about modifying data.
为什么不使用模型继承?
CustomCalendarEvent
可以继承CalendarEvent
并以这种方式添加新字段。Why don't you use model inheritance?
CustomCalendarEvent
can inherit fromCalendarEvent
and add the new fields that way.