PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/generic_inline_admin/models.py

https://code.google.com/p/mango-py/
Python | 113 lines | 62 code | 29 blank | 22 comment | 0 complexity | d0eef10c8858db24cf62350d150f7c16 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db import models
  2. from django.contrib import admin
  3. from django.contrib.contenttypes import generic
  4. from django.contrib.contenttypes.models import ContentType
  5. class Episode(models.Model):
  6. name = models.CharField(max_length=100)
  7. class Media(models.Model):
  8. """
  9. Media that can associated to any object.
  10. """
  11. content_type = models.ForeignKey(ContentType)
  12. object_id = models.PositiveIntegerField()
  13. content_object = generic.GenericForeignKey()
  14. url = models.URLField(verify_exists=False)
  15. def __unicode__(self):
  16. return self.url
  17. class MediaInline(generic.GenericTabularInline):
  18. model = Media
  19. class EpisodeAdmin(admin.ModelAdmin):
  20. inlines = [
  21. MediaInline,
  22. ]
  23. admin.site.register(Episode, EpisodeAdmin)
  24. #
  25. # These models let us test the different GenericInline settings at
  26. # different urls in the admin site.
  27. #
  28. #
  29. # Generic inline with extra = 0
  30. #
  31. class EpisodeExtra(Episode):
  32. pass
  33. class MediaExtraInline(generic.GenericTabularInline):
  34. model = Media
  35. extra = 0
  36. admin.site.register(EpisodeExtra, inlines=[MediaExtraInline])
  37. #
  38. # Generic inline with extra and max_num
  39. #
  40. class EpisodeMaxNum(Episode):
  41. pass
  42. class MediaMaxNumInline(generic.GenericTabularInline):
  43. model = Media
  44. extra = 5
  45. max_num = 2
  46. admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
  47. #
  48. # Generic inline with exclude
  49. #
  50. class EpisodeExclude(Episode):
  51. pass
  52. class MediaExcludeInline(generic.GenericTabularInline):
  53. model = Media
  54. exclude = ['url']
  55. admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
  56. #
  57. # Generic inline with unique_together
  58. #
  59. class Category(models.Model):
  60. name = models.CharField(max_length=50)
  61. class PhoneNumber(models.Model):
  62. content_type = models.ForeignKey(ContentType)
  63. object_id = models.PositiveIntegerField()
  64. content_object = generic.GenericForeignKey('content_type', 'object_id')
  65. phone_number = models.CharField(max_length=30)
  66. category = models.ForeignKey(Category, null=True, blank=True)
  67. class Meta:
  68. unique_together = (('content_type', 'object_id', 'phone_number',),)
  69. class Contact(models.Model):
  70. name = models.CharField(max_length=50)
  71. phone_numbers = generic.GenericRelation(PhoneNumber)
  72. class PhoneNumberInline(generic.GenericTabularInline):
  73. model = PhoneNumber
  74. admin.site.register(Contact, inlines=[PhoneNumberInline])
  75. admin.site.register(Category)
  76. #
  77. # Generic inline with can_delete=False
  78. #
  79. class EpisodePermanent(Episode):
  80. pass
  81. class MediaPermanentInline(generic.GenericTabularInline):
  82. model = Media
  83. can_delete = False
  84. admin.site.register(EpisodePermanent, inlines=[MediaPermanentInline])