有没有办法自定义模板中显示自定义模型字段的值?
我将日期作为YYYYMMDD格式的整数字段存储,其中月或日是可选的。
I am storing dates as an integer field in the format YYYYMMDD, where month or day is optional.
我有以下功能格式化数字:
I have the following function for formatting the number:
def flexibledateformat(value):
import datetime, re
try:
value = str(int(value))
except:
return None
match = re.match(r'(\d{4})(\d\d)(\d\d)$',str(value))
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
if day_val:
return datetime.datetime.strftime(datetime.date(year_val,month_val,day_val),'%b %e, %Y')
elif month_val:
return datetime.datetime.strftime(datetime.date(year_val,month_val,1),'%B %Y')
else:
return str(year_val)
其结果如下:
>>> flexibledateformat(20100415)
'Apr 15, 2010'
>>> flexibledateformat(20100400)
'April 2010'
>>> flexibledateformat(20100000)
'2010'
所以我想知道是否有一个功能可以在模型字段类下添加自动调用 flexibledateformat
。
So I'm wondering if there's a function I can add under the model field class that would automatically call flexibledateformat
.
所以如果有一个记录
r = DataRecord(name ='foo',date = 20100400)
在处理时的值为 20100400
,但是当使用 {{r.date}}
它显示为2010年4月。
So if there's a record
r = DataRecord(name='foo',date=20100400)
when processed in the form the value would be 20100400
but when output in a template using {{ r.date }}
it shows up as "April 2010".
我通常使用datetime来存储日期/时间值。在这个具体情况下,我需要记录非特定日期: x 发生在2009年, y 发生在1996年6月的某个时间 。
I do normally use datetime for storing date/time values. In this specific case, I need to record non-specific dates: "x happened in 2009", "y happened sometime in June 1996".
在保持日期字段的大部分功能(包括排序和过滤)的同时,最简单的方法是使用格式为 YYYYMMDD 。这就是为什么我使用一个 IntegerField
而不是一个 DateTimeField
。
The easiest way to do this while still preserving most of the functionality of a date field, including sorting and filtering, is by using an integer in the format of yyyymmdd
. That is why I am using an IntegerField
instead of a DateTimeField
.
这是我想要发生的事情:
This is what I would like to happen:
- 我存储我所说的弹性
日期FlexibleDateField
作为
整数,格式为yyyymmdd
。 - 我提交一个包含
FlexibleDateField的表单,值
保持一个整数,以便函数
用于验证它,
在窗口小部件中呈现正确的
。 - 我在一个模板中调用它,
如{{object.flexibledate}}和
,它根据
flexibledateformat规则格式化: code> 20100416
- > 2010年4月16日;20100400
- > 2010年4月;20100000
- > 2010.这个
也适用于我直接不叫
时,例如当它用作
a头admin
(http://example.org/admin/app_name/model_name/
)。
- I store what I call a "Flexible
Date" in a
FlexibleDateField
as an integer with the formatyyyymmdd
. - I render a form that includes a FlexibleDateField, and the value remains an integer so that functions necessary for validating it and rendering it in widgets work correctly.
- I call it in a template,
as in {{ object.flexibledate }} and
it is formatted according to the
flexibledateformat rules:
20100416
-> April 16, 2010;20100400
-> April 2010;20100000
-> 2010. This also applies when I'm not calling it directly, such as when it's used as a header in admin (http://example.org/admin/app_name/model_name/
).
我不知道这些具体的事情是否可行。
I'm not aware if these specific things are possible.
我已经写了一个模板过滤器,可以执行这个功能 -
( {{object.flexibledate | flexibledateformat}}
) -
每当我想输出其中一个值时,我宁可不必调用过滤器。除非输出表单,我几乎不想看到数字。
I have already written a template filter that can perform this function --
({{object.flexibledate|flexibledateformat}}
) --
but I'd rather not have to call the filter every time I want to output one of these values. Except when outputting the form, I pretty much never want to see the number.
我认为最多的是djangoish 实现类似效果的方式是在模型上定义一个自定义方法:
I think the most djangoish way of accomplishing a similar effect would be to define a custom method on a model:
class Model(models.Model):
date = models.IntegerField()
...
def flexibledate(self):
return flexibledateformat(self.date)
您可以在这样的模板中使用它:
You can use it in a template like this:
{{ r.flexibledate }}
我也想知道你为什么使用IntegerField而不是本地DateTimeField。
I also wonder why are you using an IntegerField instead of a native DateTimeField.
更新:您仍然可以使用自定义方法作为 Django Admin中的list_display
如果您希望列标题说出其他内容,那么方法的名称使用short_description选项,如下所示:
Update: You can still use the custom method as an option for list_display
in Django Admin. If you want the column header to say something else then simply the method's name use the "short_description" option, like this:
class Model(models.Model):
date = models.IntegerField()
...
def flexibledate(self):
return flexibledateformat(self.date)
flexibledate.short_description = 'Date'
然后你只要使用它一样的方式将使用普通字段:
and then you just use it the same way you would use an ordinary field:
class YourModelAdmin(admin.ModelAdmin):
list_display = ('title', 'flexibledate')
admin.site.register(YourModel, YourModelAdmin)