Re:Tips to a new django developer
by jacquesm on Monday August 31, 2009 @11:40AM
(#796119)
I'd like to add one here (and as a 'new' django developer myself take it with a grain of salt), use the save method in your subclasses of 'models.Model' to do things like computed fields and other trickery to keep your database consistent.
Otherwise you'll end up doing this in multiple places throughout your views.
If your database supports it triggers, stored procedures and functions are probably a better way of achieving most of these things.
By putting a save method in your subclass you get access to the record about to be created or updated, you can use this to lift the id field out and do tricks with it just after insertion in to the database, or you can store the value of a field that can not be easily computed during data retrieval.
I use this in an application where each page appears as a leaf in a tree to update the path to the node based on the parent node and its path.
Otherwise you'll end up doing this in multiple places throughout your views.
If your database supports it triggers, stored procedures and functions are probably a better way of achieving most of these things.
By putting a save method in your subclass you get access to the record about to be created or updated, you can use this to lift the id field out and do tricks with it just after insertion in to the database, or you can store the value of a field that can not be easily computed during data retrieval.
I use this in an application where each page appears as a leaf in a tree to update the path to the node based on the parent node and its path.