PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms 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
  1from django.db import models
  2from django.contrib import admin
  3from django.contrib.contenttypes import generic
  4from django.contrib.contenttypes.models import ContentType
  5
  6class Episode(models.Model):
  7    name = models.CharField(max_length=100)
  8
  9class Media(models.Model):
 10    """
 11    Media that can associated to any object.
 12    """
 13    content_type = models.ForeignKey(ContentType)
 14    object_id = models.PositiveIntegerField()
 15    content_object = generic.GenericForeignKey()
 16    url = models.URLField(verify_exists=False)
 17
 18    def __unicode__(self):
 19        return self.url
 20
 21class MediaInline(generic.GenericTabularInline):
 22    model = Media
 23
 24class EpisodeAdmin(admin.ModelAdmin):
 25    inlines = [
 26        MediaInline,
 27    ]
 28admin.site.register(Episode, EpisodeAdmin)
 29
 30#
 31# These models let us test the different GenericInline settings at
 32# different urls in the admin site.
 33#
 34
 35#
 36# Generic inline with extra = 0
 37#
 38
 39class EpisodeExtra(Episode):
 40    pass
 41
 42class MediaExtraInline(generic.GenericTabularInline):
 43    model = Media
 44    extra = 0
 45
 46admin.site.register(EpisodeExtra, inlines=[MediaExtraInline])
 47
 48#
 49# Generic inline with extra and max_num
 50#
 51
 52class EpisodeMaxNum(Episode):
 53    pass
 54
 55class MediaMaxNumInline(generic.GenericTabularInline):
 56    model = Media
 57    extra = 5
 58    max_num = 2
 59
 60admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
 61
 62#
 63# Generic inline with exclude
 64#
 65
 66class EpisodeExclude(Episode):
 67    pass
 68
 69class MediaExcludeInline(generic.GenericTabularInline):
 70    model = Media
 71    exclude = ['url']
 72
 73admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
 74
 75#
 76# Generic inline with unique_together
 77#
 78
 79class Category(models.Model):
 80    name = models.CharField(max_length=50)
 81
 82class PhoneNumber(models.Model):
 83    content_type = models.ForeignKey(ContentType)
 84    object_id = models.PositiveIntegerField()
 85    content_object = generic.GenericForeignKey('content_type', 'object_id')
 86    phone_number = models.CharField(max_length=30)
 87    category = models.ForeignKey(Category, null=True, blank=True)
 88
 89    class Meta:
 90        unique_together = (('content_type', 'object_id', 'phone_number',),)
 91
 92class Contact(models.Model):
 93    name = models.CharField(max_length=50)
 94    phone_numbers = generic.GenericRelation(PhoneNumber)
 95
 96class PhoneNumberInline(generic.GenericTabularInline):
 97    model = PhoneNumber
 98
 99admin.site.register(Contact, inlines=[PhoneNumberInline])
100admin.site.register(Category)
101
102#
103# Generic inline with can_delete=False
104#
105
106class EpisodePermanent(Episode):
107    pass
108
109class MediaPermanentInline(generic.GenericTabularInline):
110    model = Media
111    can_delete = False
112
113admin.site.register(EpisodePermanent, inlines=[MediaPermanentInline])