2008-11-13 19:03:44 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.contenttypes import generic
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
|
|
class Episode(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2011-08-12 14:14:58 +00:00
|
|
|
length = models.CharField(max_length=100, blank=True)
|
|
|
|
author = models.CharField(max_length=100, blank=True)
|
2008-11-13 19:03:44 +00:00
|
|
|
|
|
|
|
class Media(models.Model):
|
|
|
|
"""
|
|
|
|
Media that can associated to any object.
|
|
|
|
"""
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
content_object = generic.GenericForeignKey()
|
|
|
|
url = models.URLField(verify_exists=False)
|
2011-08-12 14:14:58 +00:00
|
|
|
description = models.CharField(max_length=100, blank=True)
|
|
|
|
keywords = models.CharField(max_length=100, blank=True)
|
2008-11-13 19:03:44 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.url
|
|
|
|
|
|
|
|
class MediaInline(generic.GenericTabularInline):
|
|
|
|
model = Media
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline,
|
|
|
|
]
|
|
|
|
admin.site.register(Episode, EpisodeAdmin)
|
2009-04-18 20:12:53 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# These models let us test the different GenericInline settings at
|
|
|
|
# different urls in the admin site.
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generic inline with extra = 0
|
|
|
|
#
|
|
|
|
|
|
|
|
class EpisodeExtra(Episode):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MediaExtraInline(generic.GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 0
|
|
|
|
|
|
|
|
admin.site.register(EpisodeExtra, inlines=[MediaExtraInline])
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generic inline with extra and max_num
|
|
|
|
#
|
|
|
|
|
|
|
|
class EpisodeMaxNum(Episode):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MediaMaxNumInline(generic.GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 5
|
|
|
|
max_num = 2
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2009-04-18 20:12:53 +00:00
|
|
|
admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
|
|
|
|
|
|
|
|
|
2010-01-21 01:40:21 +00:00
|
|
|
#
|
|
|
|
# Generic inline with unique_together
|
|
|
|
#
|
|
|
|
|
2010-11-13 18:42:31 +00:00
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2010-01-21 01:40:21 +00:00
|
|
|
class PhoneNumber(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
content_object = generic.GenericForeignKey('content_type', 'object_id')
|
|
|
|
phone_number = models.CharField(max_length=30)
|
2010-11-13 18:42:31 +00:00
|
|
|
category = models.ForeignKey(Category, null=True, blank=True)
|
2010-01-21 01:40:21 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('content_type', 'object_id', 'phone_number',),)
|
|
|
|
|
|
|
|
class Contact(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
phone_numbers = generic.GenericRelation(PhoneNumber)
|
|
|
|
|
|
|
|
class PhoneNumberInline(generic.GenericTabularInline):
|
|
|
|
model = PhoneNumber
|
|
|
|
|
|
|
|
admin.site.register(Contact, inlines=[PhoneNumberInline])
|
2010-11-13 18:42:31 +00:00
|
|
|
admin.site.register(Category)
|
2010-02-23 17:14:50 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Generic inline with can_delete=False
|
|
|
|
#
|
|
|
|
|
|
|
|
class EpisodePermanent(Episode):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MediaPermanentInline(generic.GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
can_delete = False
|
|
|
|
|
|
|
|
admin.site.register(EpisodePermanent, inlines=[MediaPermanentInline])
|