2008-12-16 17:42:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-07-18 23:54:34 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib import admin
|
|
|
|
|
|
|
|
class Section(models.Model):
|
|
|
|
"""
|
2008-08-08 18:07:33 +00:00
|
|
|
A simple section that links to articles, to test linking to related items
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
title = models.CharField(max_length=100)
|
2008-07-18 23:54:34 +00:00
|
|
|
content = models.TextField()
|
|
|
|
date = models.DateTimeField()
|
|
|
|
section = models.ForeignKey(Section)
|
|
|
|
|
2008-08-08 18:07:33 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def model_year(self):
|
|
|
|
return self.date.year
|
|
|
|
model_year.admin_order_field = 'date'
|
|
|
|
|
2008-12-16 15:04:47 +00:00
|
|
|
class Book(models.Model):
|
|
|
|
"""
|
|
|
|
A simple book that has chapters.
|
|
|
|
"""
|
2008-12-16 17:42:18 +00:00
|
|
|
name = models.CharField(max_length=100, verbose_name=u'¿Name?')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Promo(models.Model):
|
|
|
|
name = models.CharField(max_length=100, verbose_name=u'¿Name?')
|
|
|
|
book = models.ForeignKey(Book)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2008-12-16 15:04:47 +00:00
|
|
|
|
|
|
|
class Chapter(models.Model):
|
2008-12-16 17:42:18 +00:00
|
|
|
title = models.CharField(max_length=100, verbose_name=u'¿Title?')
|
2008-12-16 15:04:47 +00:00
|
|
|
content = models.TextField()
|
|
|
|
book = models.ForeignKey(Book)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
2008-12-16 17:42:18 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = u'¿Chapter?'
|
|
|
|
|
|
|
|
class ChapterXtra1(models.Model):
|
|
|
|
chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
|
2009-03-10 11:19:26 +00:00
|
|
|
xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
|
2008-12-16 17:42:18 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'¿Xtra1: %s' % self.xtra
|
|
|
|
|
|
|
|
class ChapterXtra2(models.Model):
|
|
|
|
chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
|
2009-03-10 11:19:26 +00:00
|
|
|
xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
|
2008-12-16 17:42:18 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'¿Xtra2: %s' % self.xtra
|
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def callable_year(dt_value):
|
|
|
|
return dt_value.year
|
|
|
|
callable_year.admin_order_field = 'date'
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-08-09 20:52:40 +00:00
|
|
|
class ArticleInline(admin.TabularInline):
|
|
|
|
model = Article
|
|
|
|
|
2008-12-16 15:04:47 +00:00
|
|
|
class ChapterInline(admin.TabularInline):
|
|
|
|
model = Chapter
|
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
2008-10-08 14:47:01 +00:00
|
|
|
list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
|
2008-07-18 23:54:34 +00:00
|
|
|
list_filter = ('date',)
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def changelist_view(self, request):
|
|
|
|
"Test that extra_context works"
|
|
|
|
return super(ArticleAdmin, self).changelist_view(
|
|
|
|
request, extra_context={
|
|
|
|
'extra_var': 'Hello!'
|
|
|
|
}
|
|
|
|
)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
2008-10-08 14:47:01 +00:00
|
|
|
def modeladmin_year(self, obj):
|
|
|
|
return obj.date.year
|
|
|
|
modeladmin_year.admin_order_field = 'date'
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00: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-08 18:07:33 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.id
|
2008-08-08 18:07:33 +00:00
|
|
|
|
2008-10-21 19:03:21 +00:00
|
|
|
class Color(models.Model):
|
|
|
|
value = models.CharField(max_length=10)
|
2009-03-10 11:19:26 +00:00
|
|
|
warm = models.BooleanField()
|
2008-10-21 19:03:21 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
class Thing(models.Model):
|
|
|
|
title = models.CharField(max_length=20)
|
|
|
|
color = models.ForeignKey(Color, limit_choices_to={'warm': True})
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
class ThingAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ('color',)
|
|
|
|
|
2009-03-10 11:19:26 +00:00
|
|
|
class Persona(models.Model):
|
|
|
|
"""
|
|
|
|
A simple persona associated with accounts, to test inlining of related
|
|
|
|
accounts which inherit from a common accounts class.
|
|
|
|
"""
|
|
|
|
name = models.CharField(blank=False, max_length=80)
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Account(models.Model):
|
|
|
|
"""
|
|
|
|
A simple, generic account encapsulating the information shared by all
|
|
|
|
types of accounts.
|
|
|
|
"""
|
|
|
|
username = models.CharField(blank=False, max_length=80)
|
|
|
|
persona = models.ForeignKey(Persona, related_name="accounts")
|
|
|
|
servicename = u'generic service'
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "%s: %s" % (self.servicename, self.username)
|
|
|
|
|
|
|
|
class FooAccount(Account):
|
|
|
|
"""A service-specific account of type Foo."""
|
|
|
|
servicename = u'foo'
|
|
|
|
|
|
|
|
class BarAccount(Account):
|
|
|
|
"""A service-specific account of type Bar."""
|
|
|
|
servicename = u'bar'
|
|
|
|
|
|
|
|
class FooAccountAdmin(admin.StackedInline):
|
|
|
|
model = FooAccount
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
class BarAccountAdmin(admin.StackedInline):
|
|
|
|
model = BarAccount
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
class PersonaAdmin(admin.ModelAdmin):
|
|
|
|
inlines = (
|
|
|
|
FooAccountAdmin,
|
|
|
|
BarAccountAdmin
|
|
|
|
)
|
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
admin.site.register(Article, ArticleAdmin)
|
|
|
|
admin.site.register(CustomArticle, CustomArticleAdmin)
|
2008-08-09 20:52:40 +00:00
|
|
|
admin.site.register(Section, inlines=[ArticleInline])
|
2008-07-18 23:54:34 +00:00
|
|
|
admin.site.register(ModelWithStringPrimaryKey)
|
2008-10-21 19:03:21 +00:00
|
|
|
admin.site.register(Color)
|
|
|
|
admin.site.register(Thing, ThingAdmin)
|
2009-03-10 11:19:26 +00:00
|
|
|
admin.site.register(Persona, PersonaAdmin)
|
2008-12-16 17:42:18 +00:00
|
|
|
|
|
|
|
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
|
|
|
|
# That way we cover all four cases:
|
|
|
|
# related ForeignKey object registered in admin
|
|
|
|
# related ForeignKey object not registered in admin
|
|
|
|
# related OneToOne object registered in admin
|
|
|
|
# related OneToOne object not registered in admin
|
|
|
|
# when deleting Book so as exercise all four troublesome (w.r.t escaping
|
|
|
|
# and calling force_unicode to avoid problems on Python 2.3) paths through
|
|
|
|
# contrib.admin.util's get_deleted_objects function.
|
|
|
|
admin.site.register(Book, inlines=[ChapterInline])
|
|
|
|
admin.site.register(Promo)
|
|
|
|
admin.site.register(ChapterXtra1)
|
2009-03-10 11:19:26 +00:00
|
|
|
|
|
|
|
|