I have a model in which I would like to indicate the name of the topic and its initials. (The data is somewhat anonymous and is tracked by initials.)
Right now i wrote
class Subject(models.Model): name = models.CharField("Name", max_length=30) def subject_initials(self): return ''.join(map(lambda x: '' if len(x)==0 else x[0], self.name.split(' ')))
As indicated on the last line, I would prefer that the initials are actually stored in the database as a field (regardless of the name), but it is initialized with a default value based on the name field. However, I am having problems because django models do not seem to have a self.
If I changed the line to subject_init = models.CharField("Subject initials", max_length=2, default=subject_initials) , I can do syncdb, but I can not create new themes.
Is this possible in django if the called function gives a default field based on the value of another field?
(Curiously, the reason I want to separate my store’s initials separately is, in rare cases, when strange surnames may differ from the ones I’m tracking. For example, someone else decided that topic 1 is named “John O'Mallory” The initials are "JM", not "JO", and you want to fix it as an administrator.)
dr jimbob Dec 07 '10 at 19:43 2010-12-07 19:43
source share