summaryrefslogtreecommitdiff
path: root/tests/regressiontests/generic_inline_admin/models.py
blob: 32ecd3b889a2b323f8083889ac0e42e33ffa668c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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)
    length = models.CharField(max_length=100, blank=True)
    author = models.CharField(max_length=100, blank=True)

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)
    description = models.CharField(max_length=100, blank=True)
    keywords = models.CharField(max_length=100, blank=True)

    def __unicode__(self):
        return self.url

class MediaInline(generic.GenericTabularInline):
    model = Media

class EpisodeAdmin(admin.ModelAdmin):
    inlines = [
        MediaInline,
    ]
admin.site.register(Episode, EpisodeAdmin)

#
# 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

admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])


#
# Generic inline with unique_together
#

class Category(models.Model):
    name = models.CharField(max_length=50)

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)
    category = models.ForeignKey(Category, null=True, blank=True)

    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])
admin.site.register(Category)

#
# 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])