views:

121

answers:

2

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?

+2  A: 

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 and code in CustomCalendarEvent. Updates to name will have to be synchronized to code 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() and delete() 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.

celopes
Thanks for the response. It was starting to feel like I was abusing the purpose of the Custom Manager.
Strawberry
+2  A: 

Why don't you use model inheritance? CustomCalendarEvent can inherit from CalendarEvent and add the new fields that way.

Daniel Roseman
This is also a very good idea. See http://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance
celopes
Yes... this is a good idea. I was aware Django supported inheritance with an abstract superclass but hadn't realised I could just extend existing classes - which in this case I'm not allowed to modify.
Strawberry