2008-07-19 07:54:34 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib import admin
|
|
|
|
|
|
|
|
class Section(models.Model):
|
|
|
|
"""
|
2008-08-09 02:07:33 +08:00
|
|
|
A simple section that links to articles, to test linking to related items
|
2008-07-19 07:54:34 +08:00
|
|
|
in admin views.
|
|
|
|
"""
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
"""
|
|
|
|
A simple article to test admin views. Test backwards compatibility.
|
|
|
|
"""
|
2008-08-09 02:07:33 +08:00
|
|
|
title = models.CharField(max_length=100)
|
2008-07-19 07:54:34 +08:00
|
|
|
content = models.TextField()
|
|
|
|
date = models.DateTimeField()
|
|
|
|
section = models.ForeignKey(Section)
|
|
|
|
|
2008-08-09 02:07:33 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
2008-10-08 22:47:01 +08:00
|
|
|
|
|
|
|
def model_year(self):
|
|
|
|
return self.date.year
|
|
|
|
model_year.admin_order_field = 'date'
|
|
|
|
|
|
|
|
def callable_year(dt_value):
|
|
|
|
return dt_value.year
|
|
|
|
callable_year.admin_order_field = 'date'
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-08-10 04:52:40 +08:00
|
|
|
class ArticleInline(admin.TabularInline):
|
|
|
|
model = Article
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
2008-10-08 22:47:01 +08:00
|
|
|
list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
|
2008-07-19 07:54:34 +08:00
|
|
|
list_filter = ('date',)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def changelist_view(self, request):
|
|
|
|
"Test that extra_context works"
|
|
|
|
return super(ArticleAdmin, self).changelist_view(
|
|
|
|
request, extra_context={
|
|
|
|
'extra_var': 'Hello!'
|
|
|
|
}
|
|
|
|
)
|
2008-10-08 22:47:01 +08:00
|
|
|
|
|
|
|
def modeladmin_year(self, obj):
|
|
|
|
return obj.date.year
|
|
|
|
modeladmin_year.admin_order_field = 'date'
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
class CustomArticle(models.Model):
|
|
|
|
content = models.TextField()
|
|
|
|
date = models.DateTimeField()
|
|
|
|
|
|
|
|
class CustomArticleAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
Tests various hooks for using custom templates and contexts.
|
|
|
|
"""
|
|
|
|
change_list_template = 'custom_admin/change_list.html'
|
|
|
|
change_form_template = 'custom_admin/change_form.html'
|
|
|
|
object_history_template = 'custom_admin/object_history.html'
|
|
|
|
delete_confirmation_template = 'custom_admin/delete_confirmation.html'
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def changelist_view(self, request):
|
|
|
|
"Test that extra_context works"
|
|
|
|
return super(CustomArticleAdmin, self).changelist_view(
|
|
|
|
request, extra_context={
|
|
|
|
'extra_var': 'Hello!'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
class ModelWithStringPrimaryKey(models.Model):
|
|
|
|
id = models.CharField(max_length=255, primary_key=True)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.id
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
admin.site.register(Article, ArticleAdmin)
|
|
|
|
admin.site.register(CustomArticle, CustomArticleAdmin)
|
2008-08-10 04:52:40 +08:00
|
|
|
admin.site.register(Section, inlines=[ArticleInline])
|
2008-07-19 07:54:34 +08:00
|
|
|
admin.site.register(ModelWithStringPrimaryKey)
|