开发者

datetime rendering in the admin forms

I have the following Model and ModelAdmin classes. However when I view the posts in the admin list page, the created fields are rendered as 2010-03-01 22:15:18.494594. 开发者_如何学Go I've tried setting the DATETIME_FORMAT variable in settings.py, but that didn't help. Any ideas how to control the formatting of datetime fields in the admin forms.

PS. I know I could write a method in the Post model, that does calls strftime on the created field, and render that method instead of created, but I'm sure there is a better way to handle this.

#models.py
class Post(models.Model):
  title = models.CharField(max_length=255)
  price = models.DecimalField(max_digits=11, decimal_places=2)
  description = models.TextField(blank=True)
  category = models.ForeignKey("Category")
  created = models.DateTimeField(auto_now_add=True)
  updated = models.DateTimeField(auto_now=True)

#admin.py
class PostAdmin(admin.ModelAdmin):
  list_display = ("title", "category", "price", "created",)
  list_filter = ("category",)
  search_fields = ("title", "category__name",)
admin.site.register(Post, PostAdmin)


I am not aware of a way to simply specify a date format for use in the admin interface. You do have other options though. Take a look here. According to the documentation the list of columns on the list page can include the name of a field (or simply property if I remember correctly), a callable (taking the model object as an argument), the name of a callable on the class itself that takes a model object as an argument, or the name of an instance method on the class.

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display


Odd, on my admin lists the dates are displayed like this by default:

Feb. 28, 2010, 12:25 p.m.

Have you set your locale information in settings.py?

TIME_ZONE = 'America/Los_Angeles'
LANGUAGE_CODE = 'en-us'
USE_I18N = True
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜