PageRenderTime 33ms CodeModel.GetById 24ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/admin_inlines/models.py

https://code.google.com/p/mango-py/
Python | 193 lines | 124 code | 58 blank | 11 comment | 1 complexity | 327d3447515edd6f7cb765590523f554 MD5 | raw file
  1"""
  2Testing of admin inline formsets.
  3
  4"""
  5from django.db import models
  6from django.contrib import admin
  7from django.contrib.contenttypes.models import ContentType
  8from django.contrib.contenttypes import generic
  9from django import forms
 10
 11class Parent(models.Model):
 12    name = models.CharField(max_length=50)
 13
 14    def __unicode__(self):
 15        return self.name
 16
 17class Teacher(models.Model):
 18    name = models.CharField(max_length=50)
 19
 20    def __unicode__(self):
 21        return self.name
 22
 23class Child(models.Model):
 24    name = models.CharField(max_length=50)
 25    teacher = models.ForeignKey(Teacher)
 26
 27    content_type = models.ForeignKey(ContentType)
 28    object_id = models.PositiveIntegerField()
 29    parent = generic.GenericForeignKey()
 30
 31    def __unicode__(self):
 32        return u'I am %s, a child of %s' % (self.name, self.parent)
 33
 34class Book(models.Model):
 35    name = models.CharField(max_length=50)
 36
 37class Author(models.Model):
 38    name = models.CharField(max_length=50)
 39    books = models.ManyToManyField(Book)
 40
 41class BookInline(admin.TabularInline):
 42    model = Author.books.through
 43
 44class AuthorAdmin(admin.ModelAdmin):
 45    inlines = [BookInline]
 46
 47admin.site.register(Author, AuthorAdmin)
 48
 49class Holder(models.Model):
 50    dummy = models.IntegerField()
 51
 52
 53class Inner(models.Model):
 54    dummy = models.IntegerField()
 55    holder = models.ForeignKey(Holder)
 56    readonly = models.CharField("Inner readonly label", max_length=1)
 57
 58
 59class InnerInline(admin.StackedInline):
 60    model = Inner
 61    can_delete = False
 62    readonly_fields = ('readonly',) # For bug #13174 tests.
 63
 64
 65class Holder2(models.Model):
 66    dummy = models.IntegerField()
 67
 68
 69class Inner2(models.Model):
 70    dummy = models.IntegerField()
 71    holder = models.ForeignKey(Holder2)
 72
 73class HolderAdmin(admin.ModelAdmin):
 74
 75    class Media:
 76        js = ('my_awesome_admin_scripts.js',)
 77
 78class InnerInline2(admin.StackedInline):
 79    model = Inner2
 80
 81    class Media:
 82        js = ('my_awesome_inline_scripts.js',)
 83
 84class Holder3(models.Model):
 85    dummy = models.IntegerField()
 86
 87
 88class Inner3(models.Model):
 89    dummy = models.IntegerField()
 90    holder = models.ForeignKey(Holder3)
 91
 92class InnerInline3(admin.StackedInline):
 93    model = Inner3
 94
 95    class Media:
 96        js = ('my_awesome_inline_scripts.js',)
 97
 98# Test bug #12561 and #12778
 99# only ModelAdmin media
100admin.site.register(Holder, HolderAdmin, inlines=[InnerInline])
101# ModelAdmin and Inline media
102admin.site.register(Holder2, HolderAdmin, inlines=[InnerInline2])
103# only Inline media
104admin.site.register(Holder3, inlines=[InnerInline3])
105
106# Models for #12749
107
108class Person(models.Model):
109    firstname = models.CharField(max_length=15)
110
111class OutfitItem(models.Model):
112    name = models.CharField(max_length=15)
113
114class Fashionista(models.Model):
115    person = models.OneToOneField(Person, primary_key=True)
116    weaknesses = models.ManyToManyField(OutfitItem, through='ShoppingWeakness', blank=True)
117
118class ShoppingWeakness(models.Model):
119    fashionista = models.ForeignKey(Fashionista)
120    item = models.ForeignKey(OutfitItem)
121
122class InlineWeakness(admin.TabularInline):
123    model = ShoppingWeakness
124    extra = 1
125
126admin.site.register(Fashionista, inlines=[InlineWeakness])
127
128# Models for #13510
129
130class TitleCollection(models.Model):
131    pass
132
133class Title(models.Model):
134    collection = models.ForeignKey(TitleCollection, blank=True, null=True)
135    title1 = models.CharField(max_length=100)
136    title2 = models.CharField(max_length=100)
137
138class TitleForm(forms.ModelForm):
139
140    def clean(self):
141        cleaned_data = self.cleaned_data
142        title1 = cleaned_data.get("title1")
143        title2 = cleaned_data.get("title2")
144        if title1 != title2:
145            raise forms.ValidationError("The two titles must be the same")
146        return cleaned_data
147
148class TitleInline(admin.TabularInline):
149    model = Title
150    form = TitleForm
151    extra = 1
152
153admin.site.register(TitleCollection, inlines=[TitleInline])
154
155# Models for #15424
156
157class Poll(models.Model):
158    name = models.CharField(max_length=40)
159
160class Question(models.Model):
161    poll = models.ForeignKey(Poll)
162
163class QuestionInline(admin.TabularInline):
164    model = Question
165    readonly_fields=['call_me']
166
167    def call_me(self, obj):
168        return 'Callable in QuestionInline'
169
170class PollAdmin(admin.ModelAdmin):
171    inlines = [QuestionInline]
172
173    def call_me(self, obj):
174        return 'Callable in PollAdmin'
175
176class Novel(models.Model):
177    name = models.CharField(max_length=40)
178
179class Chapter(models.Model):
180    novel = models.ForeignKey(Novel)
181
182class ChapterInline(admin.TabularInline):
183    model = Chapter
184    readonly_fields=['call_me']
185
186    def call_me(self, obj):
187        return 'Callable in ChapterInline'
188
189class NovelAdmin(admin.ModelAdmin):
190    inlines = [ChapterInline]
191
192admin.site.register(Poll, PollAdmin)
193admin.site.register(Novel, NovelAdmin)