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
6 class Episode(models
.Model
):
7 name
= models
.CharField(max_length
=100)
9 class Media(models
.Model
):
11 Media that can associated to any object.
13 content_type
= models
.ForeignKey(ContentType
)
14 object_id
= models
.PositiveIntegerField()
15 content_object
= generic
.GenericForeignKey()
16 url
= models
.URLField(verify_exists
=False)
18 def __unicode__(self
):
21 class MediaInline(generic
.GenericTabularInline
):
24 class EpisodeAdmin(admin
.ModelAdmin
):
28 admin
.site
.register(Episode
, EpisodeAdmin
)
31 # These models let us test the different GenericInline settings at
32 # different urls in the admin site.
36 # Generic inline with extra = 0
39 class EpisodeExtra(Episode
):
42 class MediaExtraInline(generic
.GenericTabularInline
):
46 admin
.site
.register(EpisodeExtra
, inlines
=[MediaExtraInline
])
49 # Generic inline with extra and max_num
52 class EpisodeMaxNum(Episode
):
55 class MediaMaxNumInline(generic
.GenericTabularInline
):
60 admin
.site
.register(EpisodeMaxNum
, inlines
=[MediaMaxNumInline
])
63 # Generic inline with exclude
66 class EpisodeExclude(Episode
):
69 class MediaExcludeInline(generic
.GenericTabularInline
):
73 admin
.site
.register(EpisodeExclude
, inlines
=[MediaExcludeInline
])
76 # Generic inline with unique_together
79 class Category(models
.Model
):
80 name
= models
.CharField(max_length
=50)
82 class 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)
90 unique_together
= (('content_type', 'object_id', 'phone_number',),)
92 class Contact(models
.Model
):
93 name
= models
.CharField(max_length
=50)
94 phone_numbers
= generic
.GenericRelation(PhoneNumber
)
96 class PhoneNumberInline(generic
.GenericTabularInline
):
99 admin
.site
.register(Contact
, inlines
=[PhoneNumberInline
])
100 admin
.site
.register(Category
)
103 # Generic inline with can_delete=False
106 class EpisodePermanent(Episode
):
109 class MediaPermanentInline(generic
.GenericTabularInline
):
113 admin
.site
.register(EpisodePermanent
, inlines
=[MediaPermanentInline
])