Django - 自定义模型方法 - 如何指定数据类型以便管理员正确格式化它?
示例:
class MyModel(models.Model):
field1=models.CharField(..)
field2=models.DateTimeField()
def today(self):
return self.field2
当我在管理站点中查看此内容时,field2 的格式与“today”字段的格式不同。
我如何告诉管理站点像对待 field2 一样对待今天?即,告诉 Django 管理员“今天”是一个 models.DateTimeField?
这是它所显示的内容:
Field2 today
April 5, 2011, 9:10 a.m. 2011-04-11 08:47:27
Example:
class MyModel(models.Model):
field1=models.CharField(..)
field2=models.DateTimeField()
def today(self):
return self.field2
When I look at this in the admin site, field2 is formatted differently than the today field.
How can I tell the admin site to treat today like it's treating field2? I.e., tell Django admin that 'today' is a models.DateTimeField?
Here is what it's showing:
Field2 today
April 5, 2011, 9:10 a.m. 2011-04-11 08:47:27
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要获取
DateTime
对象,请调用datetime.datetime.now()
而不是datetime.datetime.today()
编辑:
或使用
models .DateField()
用于field2
而不是models.DateTimeField()
:-)EDIT2:
这是解决方案:
To obtain
DateTime
object calldatetime.datetime.now()
instead ofdatetime.datetime.today()
EDIT:
Or use
models.DateField()
forfield2
instead ofmodels.DateTimeField()
:-)EDIT2:
Here is the solution:
这是一些非常非常奇怪的行为。总体猜测,这可能与 django 设置有关;特别是 DATETIME_FORMAT (及相关)设置。该框架可能会对字段进行内省,如果它们是 DateTime 类型,则根据上述设置进行渲染。
在大多数情况下,对方法的内省是没有意义的,所以如果是这种情况,我可以理解这种行为。
尝试相应地修改设置(提供不同的日期时间格式),并查看字段是否发生变化而方法是否保持不变。
编辑:
查看 django.contrib.databrowse.datastructs,有一段代码执行类似的操作:
我想象管理应用程序中会发生类似的事情,尽管我目前找不到确切的参考。
为了实现你想要的,你需要适当地格式化你的日期时间:
或者,使用@cata的评论:
That's some really really weird behaviour. At a total guess, it may have something to do with django settings; specifically the DATETIME_FORMAT (and related) settings. The framework probably does introspection on fields, and if they are of DateTime type, are rendered according to the aforementioned settings.
Introspection on methods wouldn't make sense in the majority of cases, so I could understand this behaviour if it is the case.
Try modifying the settings accordingly (provide different datetime formats), and see if the fields change and the method remains the same.
Edit:
Looking at django.contrib.databrowse.datastructures, there is a section of code that does something like:
I'd imagine a similar thing happening within the admin app, though I can't find an exact reference at the moment.
To achieve what you want, you'll need to format your datetime appropriately:
Or, using @cata's comment:
如果您选择通过自己的函数提供“list_display”项目,并且您对默认输出不满意,则需要自己对其进行格式化。在这种情况下,如果您希望具有与 DateTime 数据库字段最终的格式相同的格式:
背景:
使用模板标记
,该标记依次调用
以呈现每行的各个列值。
您将看到您的两个值一开始都是 DateTime 实例,无论是通过数据库查找还是调用您的函数,
但返回值“f”只有在存在数据库字段时才会是一个字段。您提供了一个函数,因此 Lookup_field() 将仅提供值,而“f”将为 None。
因此,在 items_for_result() 中,您的值将遍历“if f is None”块并错过
换句话说,
只会在数据库值上调用以根据字段类型进行格式化,所以这是您的函数的处理value is Missing on:
并且您需要自己完成最后一行,如上所示。
编辑:关于你的问题
它不是 models.DateTimeField,它是一个函数值。如果它是 models.DateTimeField,它将描述您的模型。看看所有需要的东西: http://docs.djangoproject.com/ en/dev/ref/models/fields/
在您的示例中,您确实可以只使用 field2。显然你想要对其价值做一些事情,计算它等等 - 那么今天的 db_column 是什么?
也就是说,如果默认情况下通过 format.localize() 运行作为 DateTime 实例的函数值,那就太好了,因为这就是本地化文档似乎所希望的。
顺便说一句,我宁愿在 ModelAdmin 中定义格式化值,而不是在模型本身中定义。我通常将其称为“formatted_today”(以保留原始 Today() 的日期时间值),只是如果管理员是唯一需要格式化值的地方,恕我直言,这就是应该定义它的地方。
If you choose to supply a "list_display" item through your own function, and you're not happy with the default output, you'll need to format it yourself. In this case, if you want to have identical formatting to what the DateTime database field ends up with:
Background:
uses the template tag
which in turn will call
to render the individual column values for each row.
You'll see that both your values start off being a DateTime instance, either through database lookup or calling your function, see
but the return value "f" will only be a field if there was a database field. You provided a function, so lookup_field() will only provide the value, and "f" will be None.
So in items_for_result(), your value will run through the "if f is None" block and miss out on
In other words,
will only be called on the database value to format according to the field type, so this is the treatment your function value is missing out on:
and you'll need to do that last line yourself, as shown above.
EDIT: Regarding your question
It's not a models.DateTimeField, it's a function value. If it were a models.DateTimeField, it would be describing your model. Look at all the stuff that entails: http://docs.djangoproject.com/en/dev/ref/models/fields/
In your example, you really could just use field2. Apparently you want to do things to its value, calculate it etc. - so what's today.db_column then?
That said, it would be nice if function values that are DateTime instances were run through format.localize() by default, as that's what the documentation on localization seems to be promising.
By the way, I would rather define a formatted value in the ModelAdmin than in the model itself. I usually call it something like "formatted_today" (to keep the datetime value of the original today()), it's just that if the Admin is the only place that needs the formatted value, imho that's where it should be defined.
之前的所有答案都提供了解决方案,这些解决方案将在新的 Django 版本中错误地处理时区信息。
如果
settings.USE_TZ==True
,field2
和today
通常会显示不同的时间。我今天发现这个问题并花了一些时间找出正确的方法:
All previous answers provide solutions, that will handle timezone info incorrectly in new Django versions.
field2
andtoday
will generally show different time ifsettings.USE_TZ==True
.I found this question today and have spent some time to figure out the correct way: