I generally love Django, but it does have some real holes. The worst problem for me is its complete lack of support for database changes. The Django devolppers argue, maybe convincingly, that database evolution is delicate enough that it’s dangerous to automate. But I feel like it’s dangerous enough it shouldn’t all be left to me either. I’m not particularly scared of SQL per se, but I know I could easily get something wrong. Do Rails’ schema evolution things make that easier, or at least less scary?
I just added “created” and “modified” date fields to one of my models, both using Django’s automated date fields, that set themselves to the current date when an object is added/modified. That led to an extra complication: I wanted the real dates for all my already-existing data. Fortunately Django’s admin keeps a log from which all the modification dates can be extracted. Once gotten, they can’t be saved to the database through Django’s normal model inerface–as that would be a modification that would auto-set the modification date, exactly what I don’t want to do. So that required a bit more SQL.
OK, despite my whingeing the SQL for that was pretty trivial. I’ll suck it up now. The interesting bit was the python function for extracting dates from logs:
def getdate(obj, addition): """ Look at the logs to find creation/modfication dates. """ model = type(obj) # we need the contenttype ct = ContentType.objects.get_for_model(model) # now we can look up the logs for this object logs = LogEntry.objects.filter(content_type=ct, object_id=obj.id).order_by('-action_time') dates = [l.action_time.date() for l in logs \ if not addition or l.is_addition() ] try: return dates except IndexError: return None