models.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from django.contrib import admin
  4. from django.core.mail import EmailMessage
  5. class Section(models.Model):
  6. """
  7. A simple section that links to articles, to test linking to related items
  8. in admin views.
  9. """
  10. name = models.CharField(max_length=100)
  11. class Article(models.Model):
  12. """
  13. A simple article to test admin views. Test backwards compatibility.
  14. """
  15. title = models.CharField(max_length=100)
  16. content = models.TextField()
  17. date = models.DateTimeField()
  18. section = models.ForeignKey(Section)
  19. def __unicode__(self):
  20. return self.title
  21. def model_year(self):
  22. return self.date.year
  23. model_year.admin_order_field = 'date'
  24. class Book(models.Model):
  25. """
  26. A simple book that has chapters.
  27. """
  28. name = models.CharField(max_length=100, verbose_name=u'¿Name?')
  29. def __unicode__(self):
  30. return self.name
  31. class Promo(models.Model):
  32. name = models.CharField(max_length=100, verbose_name=u'¿Name?')
  33. book = models.ForeignKey(Book)
  34. def __unicode__(self):
  35. return self.name
  36. class Chapter(models.Model):
  37. title = models.CharField(max_length=100, verbose_name=u'¿Title?')
  38. content = models.TextField()
  39. book = models.ForeignKey(Book)
  40. def __unicode__(self):
  41. return self.title
  42. class Meta:
  43. verbose_name = u'¿Chapter?'
  44. class ChapterXtra1(models.Model):
  45. chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
  46. xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
  47. def __unicode__(self):
  48. return u'¿Xtra1: %s' % self.xtra
  49. class ChapterXtra2(models.Model):
  50. chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
  51. xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
  52. def __unicode__(self):
  53. return u'¿Xtra2: %s' % self.xtra
  54. def callable_year(dt_value):
  55. return dt_value.year
  56. callable_year.admin_order_field = 'date'
  57. class ArticleInline(admin.TabularInline):
  58. model = Article
  59. class ChapterInline(admin.TabularInline):
  60. model = Chapter
  61. class ArticleAdmin(admin.ModelAdmin):
  62. list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
  63. list_filter = ('date',)
  64. def changelist_view(self, request):
  65. "Test that extra_context works"
  66. return super(ArticleAdmin, self).changelist_view(
  67. request, extra_context={
  68. 'extra_var': 'Hello!'
  69. }
  70. )
  71. def modeladmin_year(self, obj):
  72. return obj.date.year
  73. modeladmin_year.admin_order_field = 'date'
  74. class CustomArticle(models.Model):
  75. content = models.TextField()
  76. date = models.DateTimeField()
  77. class CustomArticleAdmin(admin.ModelAdmin):
  78. """
  79. Tests various hooks for using custom templates and contexts.
  80. """
  81. change_list_template = 'custom_admin/change_list.html'
  82. change_form_template = 'custom_admin/change_form.html'
  83. object_history_template = 'custom_admin/object_history.html'
  84. delete_confirmation_template = 'custom_admin/delete_confirmation.html'
  85. def changelist_view(self, request):
  86. "Test that extra_context works"
  87. return super(CustomArticleAdmin, self).changelist_view(
  88. request, extra_context={
  89. 'extra_var': 'Hello!'
  90. }
  91. )
  92. class ModelWithStringPrimaryKey(models.Model):
  93. id = models.CharField(max_length=255, primary_key=True)
  94. def __unicode__(self):
  95. return self.id
  96. class Color(models.Model):
  97. value = models.CharField(max_length=10)
  98. warm = models.BooleanField()
  99. def __unicode__(self):
  100. return self.value
  101. class Thing(models.Model):
  102. title = models.CharField(max_length=20)
  103. color = models.ForeignKey(Color, limit_choices_to={'warm': True})
  104. def __unicode__(self):
  105. return self.title
  106. class ThingAdmin(admin.ModelAdmin):
  107. list_filter = ('color',)
  108. class Person(models.Model):
  109. GENDER_CHOICES = (
  110. (1, "Male"),
  111. (2, "Female"),
  112. )
  113. name = models.CharField(max_length=100)
  114. gender = models.IntegerField(choices=GENDER_CHOICES)
  115. alive = models.BooleanField()
  116. def __unicode__(self):
  117. return self.name
  118. class Meta:
  119. ordering = ["id"]
  120. class PersonAdmin(admin.ModelAdmin):
  121. list_display = ('name', 'gender', 'alive')
  122. list_editable = ('gender', 'alive')
  123. list_filter = ('gender',)
  124. search_fields = ('name',)
  125. ordering = ["id"]
  126. class Persona(models.Model):
  127. """
  128. A simple persona associated with accounts, to test inlining of related
  129. accounts which inherit from a common accounts class.
  130. """
  131. name = models.CharField(blank=False, max_length=80)
  132. def __unicode__(self):
  133. return self.name
  134. class Account(models.Model):
  135. """
  136. A simple, generic account encapsulating the information shared by all
  137. types of accounts.
  138. """
  139. username = models.CharField(blank=False, max_length=80)
  140. persona = models.ForeignKey(Persona, related_name="accounts")
  141. servicename = u'generic service'
  142. def __unicode__(self):
  143. return "%s: %s" % (self.servicename, self.username)
  144. class FooAccount(Account):
  145. """A service-specific account of type Foo."""
  146. servicename = u'foo'
  147. class BarAccount(Account):
  148. """A service-specific account of type Bar."""
  149. servicename = u'bar'
  150. class FooAccountAdmin(admin.StackedInline):
  151. model = FooAccount
  152. extra = 1
  153. class BarAccountAdmin(admin.StackedInline):
  154. model = BarAccount
  155. extra = 1
  156. class PersonaAdmin(admin.ModelAdmin):
  157. inlines = (
  158. FooAccountAdmin,
  159. BarAccountAdmin
  160. )
  161. class Subscriber(models.Model):
  162. name = models.CharField(blank=False, max_length=80)
  163. email = models.EmailField(blank=False, max_length=175)
  164. def __unicode__(self):
  165. return "%s (%s)" % (self.name, self.email)
  166. class SubscriberAdmin(admin.ModelAdmin):
  167. actions = ['delete_selected', 'mail_admin']
  168. def mail_admin(self, request, selected):
  169. EmailMessage(
  170. 'Greetings from a ModelAdmin action',
  171. 'This is the test email from a admin action',
  172. 'from@example.com',
  173. ['to@example.com']
  174. ).send()
  175. class ExternalSubscriber(Subscriber):
  176. pass
  177. def external_mail(request, selected):
  178. EmailMessage(
  179. 'Greetings from a function action',
  180. 'This is the test email from a function action',
  181. 'from@example.com',
  182. ['to@example.com']
  183. ).send()
  184. def redirect_to(request, selected):
  185. from django.http import HttpResponseRedirect
  186. return HttpResponseRedirect('/some-where-else/')
  187. class ExternalSubscriberAdmin(admin.ModelAdmin):
  188. actions = [external_mail, redirect_to]
  189. admin.site.register(Article, ArticleAdmin)
  190. admin.site.register(CustomArticle, CustomArticleAdmin)
  191. admin.site.register(Section, inlines=[ArticleInline])
  192. admin.site.register(ModelWithStringPrimaryKey)
  193. admin.site.register(Color)
  194. admin.site.register(Thing, ThingAdmin)
  195. admin.site.register(Person, PersonAdmin)
  196. admin.site.register(Persona, PersonaAdmin)
  197. admin.site.register(Subscriber, SubscriberAdmin)
  198. admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
  199. # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
  200. # That way we cover all four cases:
  201. # related ForeignKey object registered in admin
  202. # related ForeignKey object not registered in admin
  203. # related OneToOne object registered in admin
  204. # related OneToOne object not registered in admin
  205. # when deleting Book so as exercise all four troublesome (w.r.t escaping
  206. # and calling force_unicode to avoid problems on Python 2.3) paths through
  207. # contrib.admin.util's get_deleted_objects function.
  208. admin.site.register(Book, inlines=[ChapterInline])
  209. admin.site.register(Promo)
  210. admin.site.register(ChapterXtra1)