|
1 """ |
|
2 Testing of admin inline formsets. |
|
3 |
|
4 """ |
|
5 from django.db import models |
|
6 from django.contrib import admin |
|
7 from django.contrib.contenttypes.models import ContentType |
|
8 from django.contrib.contenttypes import generic |
|
9 |
|
10 class Parent(models.Model): |
|
11 name = models.CharField(max_length=50) |
|
12 |
|
13 def __unicode__(self): |
|
14 return self.name |
|
15 |
|
16 class Teacher(models.Model): |
|
17 name = models.CharField(max_length=50) |
|
18 |
|
19 def __unicode__(self): |
|
20 return self.name |
|
21 |
|
22 class Child(models.Model): |
|
23 name = models.CharField(max_length=50) |
|
24 teacher = models.ForeignKey(Teacher) |
|
25 |
|
26 content_type = models.ForeignKey(ContentType) |
|
27 object_id = models.PositiveIntegerField() |
|
28 parent = generic.GenericForeignKey() |
|
29 |
|
30 def __unicode__(self): |
|
31 return u'I am %s, a child of %s' % (self.name, self.parent) |
|
32 |
|
33 class Book(models.Model): |
|
34 name = models.CharField(max_length=50) |
|
35 |
|
36 class Author(models.Model): |
|
37 name = models.CharField(max_length=50) |
|
38 books = models.ManyToManyField(Book) |
|
39 |
|
40 class BookInline(admin.TabularInline): |
|
41 model = Author.books.through |
|
42 |
|
43 class AuthorAdmin(admin.ModelAdmin): |
|
44 inlines = [BookInline] |
|
45 |
|
46 admin.site.register(Author, AuthorAdmin) |
|
47 |
|
48 class Holder(models.Model): |
|
49 dummy = models.IntegerField() |
|
50 |
|
51 |
|
52 class Inner(models.Model): |
|
53 dummy = models.IntegerField() |
|
54 holder = models.ForeignKey(Holder) |
|
55 readonly = models.CharField("Inner readonly label", max_length=1) |
|
56 |
|
57 |
|
58 class InnerInline(admin.StackedInline): |
|
59 model = Inner |
|
60 can_delete = False |
|
61 readonly_fields = ('readonly',) # For bug #13174 tests. |
|
62 |
|
63 |
|
64 class Holder2(models.Model): |
|
65 dummy = models.IntegerField() |
|
66 |
|
67 |
|
68 class Inner2(models.Model): |
|
69 dummy = models.IntegerField() |
|
70 holder = models.ForeignKey(Holder2) |
|
71 |
|
72 class HolderAdmin(admin.ModelAdmin): |
|
73 |
|
74 class Media: |
|
75 js = ('my_awesome_admin_scripts.js',) |
|
76 |
|
77 class InnerInline2(admin.StackedInline): |
|
78 model = Inner2 |
|
79 |
|
80 class Media: |
|
81 js = ('my_awesome_inline_scripts.js',) |
|
82 |
|
83 class Holder3(models.Model): |
|
84 dummy = models.IntegerField() |
|
85 |
|
86 |
|
87 class Inner3(models.Model): |
|
88 dummy = models.IntegerField() |
|
89 holder = models.ForeignKey(Holder3) |
|
90 |
|
91 class InnerInline3(admin.StackedInline): |
|
92 model = Inner3 |
|
93 |
|
94 class Media: |
|
95 js = ('my_awesome_inline_scripts.js',) |
|
96 |
|
97 # Test bug #12561 and #12778 |
|
98 # only ModelAdmin media |
|
99 admin.site.register(Holder, HolderAdmin, inlines=[InnerInline]) |
|
100 # ModelAdmin and Inline media |
|
101 admin.site.register(Holder2, HolderAdmin, inlines=[InnerInline2]) |
|
102 # only Inline media |
|
103 admin.site.register(Holder3, inlines=[InnerInline3]) |
|
104 |
|
105 # Models for #12749 |
|
106 |
|
107 class Person(models.Model): |
|
108 firstname = models.CharField(max_length=15) |
|
109 |
|
110 class OutfitItem(models.Model): |
|
111 name = models.CharField(max_length=15) |
|
112 |
|
113 class Fashionista(models.Model): |
|
114 person = models.OneToOneField(Person, primary_key=True) |
|
115 weaknesses = models.ManyToManyField(OutfitItem, through='ShoppingWeakness', blank=True) |
|
116 |
|
117 class ShoppingWeakness(models.Model): |
|
118 fashionista = models.ForeignKey(Fashionista) |
|
119 item = models.ForeignKey(OutfitItem) |
|
120 |
|
121 class InlineWeakness(admin.TabularInline): |
|
122 model = ShoppingWeakness |
|
123 extra = 1 |
|
124 |
|
125 admin.site.register(Fashionista, inlines=[InlineWeakness]) |