【发布时间】:2012-02-12 21:24:42
【问题描述】:
我正在尝试使用主键“id”填充名为“标识”的字段。但是,如您所知,在保存对象之前无法知道对象的“id”是什么。因此,我固执地这样做了:
def save(self, *args, **kwargs):
super(Notifications, self).save(*args, **kwargs)
self.identification = str(self.id)
有趣的是,这在控制台中有效:
>>>new = Notifications.objects.create( # some fields to be filled )
>>>new.identification
'3' (# or whatever number)
但是当我去我的模板检索这个对象时:
{% for each in notifications %}
Identification: {{ each.identification }}
{% endfor %}
现实打击:
Identification:
发生了什么?为什么它在控制台中工作而不是在模板中工作?您建议使用什么方法在其他字段中使用自动填充字段?
非常感谢!
【问题讨论】:
-
出于好奇,你为什么要这样做?
-
实现嵌套的 cmets 使用 1 作为父级,1.1、1.2 等用于子级,1.1.1 用于子级的子级等。这是 joomla 使用的系统。
标签: django django-models save