~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/admin_validation/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
from django.db import models
6
6
 
 
7
 
7
8
class Album(models.Model):
8
9
    title = models.CharField(max_length=150)
9
10
 
 
11
 
10
12
class Song(models.Model):
11
13
    title = models.CharField(max_length=150)
12
14
    album = models.ForeignKey(Album)
 
15
    original_release = models.DateField(editable=False)
13
16
 
14
17
    class Meta:
15
18
        ordering = ('title',)
17
20
    def __unicode__(self):
18
21
        return self.title
19
22
 
 
23
    def readonly_method_on_model(self):
 
24
        # does nothing
 
25
        pass
 
26
 
 
27
 
 
28
class TwoAlbumFKAndAnE(models.Model):
 
29
    album1 = models.ForeignKey(Album, related_name="album1_set")
 
30
    album2 = models.ForeignKey(Album, related_name="album2_set")
 
31
    e = models.CharField(max_length=1)
 
32
 
 
33
 
 
34
class Author(models.Model):
 
35
    name = models.CharField(max_length=100)
 
36
 
 
37
 
 
38
class Book(models.Model):
 
39
    name = models.CharField(max_length=100)
 
40
    subtitle = models.CharField(max_length=100)
 
41
    price = models.FloatField()
 
42
    authors = models.ManyToManyField(Author, through='AuthorsBooks')
 
43
 
 
44
 
 
45
class AuthorsBooks(models.Model):
 
46
    author = models.ForeignKey(Author)
 
47
    book = models.ForeignKey(Book)
 
48
 
 
49
 
20
50
__test__ = {'API_TESTS':"""
21
51
 
22
52
>>> from django import forms
23
53
>>> from django.contrib import admin
24
 
>>> from django.contrib.admin.validation import validate
 
54
>>> from django.contrib.admin.validation import validate, validate_inline
25
55
 
26
56
# Regression test for #8027: custom ModelForms with fields/fieldsets
27
57
 
42
72
    ...
43
73
ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is missing from the form.
44
74
 
 
75
# Tests for basic validation of 'exclude' option values (#12689)
 
76
 
 
77
>>> class ExcludedFields1(admin.ModelAdmin):
 
78
...     exclude = ('foo')
 
79
 
 
80
>>> validate(ExcludedFields1, Book)
 
81
Traceback (most recent call last):
 
82
    ...
 
83
ImproperlyConfigured: 'ExcludedFields1.exclude' must be a list or tuple.
 
84
 
 
85
>>> class ExcludedFields2(admin.ModelAdmin):
 
86
...     exclude = ('name', 'name')
 
87
 
 
88
>>> validate(ExcludedFields2, Book)
 
89
Traceback (most recent call last):
 
90
    ...
 
91
ImproperlyConfigured: There are duplicate field(s) in ExcludedFields2.exclude
 
92
 
 
93
>>> class ExcludedFieldsInline(admin.TabularInline):
 
94
...     model = Song
 
95
...     exclude = ('foo')
 
96
 
 
97
>>> class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
 
98
...     model = Album
 
99
...     inlines = [ExcludedFieldsInline]
 
100
 
 
101
>>> validate(ExcludedFieldsAlbumAdmin, Album)
 
102
Traceback (most recent call last):
 
103
    ...
 
104
ImproperlyConfigured: 'ExcludedFieldsInline.exclude' must be a list or tuple.
 
105
 
45
106
# Regression test for #9932 - exclude in InlineModelAdmin
46
107
# should not contain the ForeignKey field used in ModelAdmin.model
47
108
 
58
119
    ...
59
120
ImproperlyConfigured: SongInline cannot exclude the field 'album' - this is the foreign key to the parent model Album.
60
121
 
 
122
# Regression test for #11709 - when testing for fk excluding (when exclude is
 
123
# given) make sure fk_name is honored or things blow up when there is more
 
124
# than one fk to the parent model.
 
125
 
 
126
>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
 
127
...     model = TwoAlbumFKAndAnE
 
128
...     exclude = ("e",)
 
129
...     fk_name = "album1"
 
130
 
 
131
>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
 
132
 
 
133
# Ensure inlines validate that they can be used correctly.
 
134
 
 
135
>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
 
136
...     model = TwoAlbumFKAndAnE
 
137
 
 
138
>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
 
139
Traceback (most recent call last):
 
140
    ...
 
141
Exception: <class 'regressiontests.admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'regressiontests.admin_validation.models.Album'>
 
142
 
 
143
>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
 
144
...     model = TwoAlbumFKAndAnE
 
145
...     fk_name = "album1"
 
146
 
 
147
>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
 
148
 
 
149
>>> class SongAdmin(admin.ModelAdmin):
 
150
...     readonly_fields = ("title",)
 
151
 
 
152
>>> validate(SongAdmin, Song)
 
153
 
 
154
>>> def my_function(obj):
 
155
...     # does nothing
 
156
...     pass
 
157
>>> class SongAdmin(admin.ModelAdmin):
 
158
...     readonly_fields = (my_function,)
 
159
 
 
160
>>> validate(SongAdmin, Song)
 
161
 
 
162
>>> class SongAdmin(admin.ModelAdmin):
 
163
...     readonly_fields = ("readonly_method_on_modeladmin",)
 
164
...
 
165
...     def readonly_method_on_modeladmin(self, obj):
 
166
...         # does nothing
 
167
...         pass
 
168
 
 
169
>>> validate(SongAdmin, Song)
 
170
 
 
171
>>> class SongAdmin(admin.ModelAdmin):
 
172
...     readonly_fields = ("readonly_method_on_model",)
 
173
 
 
174
>>> validate(SongAdmin, Song)
 
175
 
 
176
>>> class SongAdmin(admin.ModelAdmin):
 
177
...     readonly_fields = ("title", "nonexistant")
 
178
 
 
179
>>> validate(SongAdmin, Song)
 
180
Traceback (most recent call last):
 
181
    ...
 
182
ImproperlyConfigured: SongAdmin.readonly_fields[1], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.
 
183
 
 
184
>>> class SongAdmin(admin.ModelAdmin):
 
185
...     readonly_fields = ("title", "awesome_song")
 
186
...     fields = ("album", "title", "awesome_song")
 
187
 
 
188
>>> validate(SongAdmin, Song)
 
189
Traceback (most recent call last):
 
190
    ...
 
191
ImproperlyConfigured: SongAdmin.readonly_fields[1], 'awesome_song' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.
 
192
 
 
193
>>> class SongAdmin(SongAdmin):
 
194
...     def awesome_song(self, instance):
 
195
...         if instance.title == "Born to Run":
 
196
...             return "Best Ever!"
 
197
...         return "Status unknown."
 
198
 
 
199
>>> validate(SongAdmin, Song)
 
200
 
 
201
>>> class SongAdmin(admin.ModelAdmin):
 
202
...     readonly_fields = (lambda obj: "test",)
 
203
 
 
204
>>> validate(SongAdmin, Song)
 
205
 
 
206
# Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
 
207
# specifies the 'through' option is included in the 'fields' or the 'fieldsets'
 
208
# ModelAdmin options.
 
209
 
 
210
>>> class BookAdmin(admin.ModelAdmin):
 
211
...     fields = ['authors']
 
212
 
 
213
>>> validate(BookAdmin, Book)
 
214
Traceback (most recent call last):
 
215
    ...
 
216
ImproperlyConfigured: 'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.
 
217
 
 
218
>>> class FieldsetBookAdmin(admin.ModelAdmin):
 
219
...     fieldsets = (
 
220
...         ('Header 1', {'fields': ('name',)}),
 
221
...         ('Header 2', {'fields': ('authors',)}),
 
222
...     )
 
223
 
 
224
>>> validate(FieldsetBookAdmin, Book)
 
225
Traceback (most recent call last):
 
226
   ...
 
227
ImproperlyConfigured: 'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.
 
228
 
 
229
>>> class NestedFieldsetAdmin(admin.ModelAdmin):
 
230
...    fieldsets = (
 
231
...        ('Main', {'fields': ('price', ('name', 'subtitle'))}),
 
232
...    )
 
233
 
 
234
>>> validate(NestedFieldsetAdmin, Book)
 
235
 
 
236
# Regression test for #12209 -- If the explicitly provided through model
 
237
# is specified as a string, the admin should still be able use
 
238
# Model.m2m_field.through
 
239
 
 
240
>>> class AuthorsInline(admin.TabularInline):
 
241
...     model = Book.authors.through
 
242
 
 
243
>>> class BookAdmin(admin.ModelAdmin):
 
244
...     inlines = [AuthorsInline]
 
245
 
 
246
# If the through model is still a string (and hasn't been resolved to a model)
 
247
# the validation will fail.
 
248
>>> validate(BookAdmin, Book)
 
249
 
 
250
# Regression for ensuring ModelAdmin.fields can contain non-model fields
 
251
# that broke with r11737
 
252
 
 
253
>>> class SongForm(forms.ModelForm):
 
254
...     extra_data = forms.CharField()
 
255
...     class Meta:
 
256
...         model = Song
 
257
 
 
258
>>> class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
 
259
...     form = SongForm
 
260
...     fields = ['title', 'extra_data']
 
261
 
 
262
>>> validate(FieldsOnFormOnlyAdmin, Song)
 
263
 
61
264
"""}