Questions tagged «django-1.2»

4
Django模型字段默认基于同一模型中的另一个字段
我有一个模型,希望包含一个科目名称及其首字母(他的数据有些匿名,并且通过首字母进行跟踪)。 现在,我写道 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(' '))) # Next line is what I want to do (or something equivalent), but doesn't work with # NameError: name 'self' is not defined subject_init = models.CharField("Subject Initials", max_length=5, default=self.subject_initials) 如最后一行所示,我希望能够将姓名的缩写实际作为字段(与名称无关)存储在数据库中,但是会使用基于名称字段的默认值进行初始化。但是,由于Django模型似乎没有“自我”,我遇到了问题。 …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.