質問編集履歴
2
更新2
test
CHANGED
File without changes
|
test
CHANGED
@@ -259,3 +259,33 @@
|
|
259
259
|
</table>
|
260
260
|
|
261
261
|
```
|
262
|
+
|
263
|
+
結果
|
264
|
+
|
265
|
+
|クラス|担任|出席番号|性別|名前|点数|
|
266
|
+
|
267
|
+
|:---:|:---:|:---:|:---:|:---:|:---:|
|
268
|
+
|
269
|
+
|1|I先生|1|男|Aくん|60|
|
270
|
+
|
271
|
+
|1|I先生|2|男|Bくん|45|
|
272
|
+
|
273
|
+
|1|I先生|3|男|Cくん|100|
|
274
|
+
|
275
|
+
|1|I先生|4|女|Dさん|60|
|
276
|
+
|
277
|
+
|1|I先生|5|女|Eさん|60|
|
278
|
+
|
279
|
+
|1|I先生|6|女|Fさん|90|
|
280
|
+
|
281
|
+
|2|J先生|1|男|イくん|55|
|
282
|
+
|
283
|
+
|2|J先生|2|男|ロくん|80|
|
284
|
+
|
285
|
+
|2|J先生|3|男|ハくん|60|
|
286
|
+
|
287
|
+
|2|J先生|4|女|二さん|40|
|
288
|
+
|
289
|
+
|2|J先生|5|女|ホさん|60|
|
290
|
+
|
291
|
+
|2|J先生|6|女|へさん|70|
|
1
誤って書きかけを投稿してしまったので更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,15 +1,69 @@
|
|
1
|
+
#困っているところ
|
2
|
+
|
3
|
+
Djangoを使ったアプリ開発に挑戦しているのですが、テーブルの並び替えと分割の仕方がわからず思うようにできません。ひとまず、簡単な例に置き換えて挑戦しています。以下の問題でわかる方がいらっしゃればご教授いただければ幸いです。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
#環境
|
2
10
|
|
3
11
|
python 3.9.5
|
4
12
|
|
5
13
|
Django 3.2.4
|
6
14
|
|
15
|
+
|
16
|
+
|
7
|
-
#
|
17
|
+
#モデル
|
8
18
|
|
9
19
|
アプリが以下の二つのテーブルを有するとします。
|
10
20
|
|
11
21
|
|
12
22
|
|
23
|
+
```django
|
24
|
+
|
25
|
+
#===================================================================
|
26
|
+
|
27
|
+
#models.py
|
28
|
+
|
29
|
+
#===================================================================
|
30
|
+
|
31
|
+
from django.db import models
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
# Create your models here.
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
List = (('Male','男'),('Female','女'))
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
class ClassModel (models.Model):
|
44
|
+
|
45
|
+
ClassNumber = models.IntegerField()
|
46
|
+
|
47
|
+
HomeroomTeacher = models.CharField(max_length=20)
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
class MenberModel (models.Model):
|
52
|
+
|
53
|
+
BelongClass = models.ForeignKey(ClassModel,on_delete=models.CASCADE)
|
54
|
+
|
55
|
+
ForM = models.CharField(choices=List,max_length= 6,default='Male')
|
56
|
+
|
57
|
+
Number = models.IntegerField()
|
58
|
+
|
59
|
+
Name = models.CharField(max_length=20)
|
60
|
+
|
61
|
+
Point = models.IntegerField()
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
13
67
|
|
14
68
|
|
15
69
|
・クラステーブル
|
@@ -120,130 +174,88 @@
|
|
120
174
|
|
121
175
|
|
122
176
|
|
123
|
-
|
124
|
-
|
125
|
-
#
|
177
|
+
#試したこと
|
178
|
+
|
126
|
-
|
179
|
+
以下のように、#1の個所で、「order_by」を使って並び替えをしてみました。うまくいっているように見えたのですがこれでは、「ClassModel」のidで並び替えられただけで、ClassModelのClassNumberでは並び変わりませんでした。
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
127
|
-
```django
|
185
|
+
``` django
|
128
|
-
|
186
|
+
|
129
|
-
#===================================================================
|
187
|
+
#===================================================================
|
130
|
-
|
188
|
+
|
131
|
-
#
|
189
|
+
#views.py
|
132
|
-
|
190
|
+
|
133
|
-
#===================================================================
|
191
|
+
#===================================================================
|
134
|
-
|
192
|
+
|
135
|
-
from django.
|
193
|
+
from django.shortcuts import render
|
136
|
-
|
137
|
-
|
138
|
-
|
194
|
+
|
139
|
-
|
195
|
+
from .models import ClassModel,MenberModel
|
196
|
+
|
197
|
+
|
198
|
+
|
140
|
-
|
199
|
+
def tabletest(request):
|
141
|
-
|
142
|
-
|
200
|
+
|
143
|
-
|
201
|
+
object_list = MenberModel.objects.order_by('BelongClass','Number') #1
|
144
|
-
|
145
|
-
|
146
|
-
|
202
|
+
|
147
|
-
cl
|
203
|
+
object_list2 = ClassModel.objects.all
|
148
|
-
|
149
|
-
|
204
|
+
|
150
|
-
|
151
|
-
HomeroomTeacher = models.CharField(max_length=20)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
class MenberModel (models.Model):
|
156
|
-
|
157
|
-
BelongClass = models.ForeignKey(ClassModel,on_delete=models.CASCADE)
|
158
|
-
|
159
|
-
|
205
|
+
return render(request, 'testapp2.html',{'object_list':object_list})
|
160
|
-
|
161
|
-
|
206
|
+
|
162
|
-
|
163
|
-
|
207
|
+
|
164
|
-
|
165
|
-
Point = models.IntegerField()
|
166
208
|
|
167
209
|
```
|
168
210
|
|
169
211
|
|
170
212
|
|
171
|
-
|
213
|
+
```html
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
<table border = '1'>
|
218
|
+
|
219
|
+
<tr>
|
220
|
+
|
221
|
+
<th>クラス</th>
|
222
|
+
|
223
|
+
<th>担任</th>
|
224
|
+
|
225
|
+
<th>出席番号</th>
|
226
|
+
|
227
|
+
<th>性別</th>
|
228
|
+
|
229
|
+
<th>名前</th>
|
230
|
+
|
231
|
+
<th>点数</th>
|
232
|
+
|
233
|
+
</tr>
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
{% for Object in object_list %}
|
238
|
+
|
239
|
+
<tr>
|
240
|
+
|
241
|
+
<td>{{Object.BelongClass.ClassNumber}}</td>
|
242
|
+
|
243
|
+
<td>{{Object.BelongClass.HomeroomTeacher}}</td>
|
244
|
+
|
245
|
+
<td>{{Object.Number}}</td>
|
246
|
+
|
247
|
+
<td>{{Object.get_ForM_display}}</td>
|
248
|
+
|
249
|
+
<td>{{Object.Name}}</td>
|
250
|
+
|
251
|
+
<td>{{Object.Point}}</td>
|
252
|
+
|
253
|
+
</tr>
|
254
|
+
|
255
|
+
{% endfor %}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
</table>
|
172
260
|
|
173
261
|
```
|
174
|
-
|
175
|
-
#===================================================================
|
176
|
-
|
177
|
-
#views.py
|
178
|
-
|
179
|
-
#===================================================================
|
180
|
-
|
181
|
-
from django.shortcuts import render
|
182
|
-
|
183
|
-
from .models import ClassModel,MenberModel
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
def tabletest(request):
|
188
|
-
|
189
|
-
object_list = MenberModel.objects.order_by('BelongClass','Number')
|
190
|
-
|
191
|
-
object_list2 = ClassModel.objects.all
|
192
|
-
|
193
|
-
return render(request, 'testapp2.html',{'object_list':object_list})
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
```
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
```html
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
<table border = '1'>
|
206
|
-
|
207
|
-
<tr>
|
208
|
-
|
209
|
-
<th>クラス</th>
|
210
|
-
|
211
|
-
<th>担任</th>
|
212
|
-
|
213
|
-
<th>出席番号</th>
|
214
|
-
|
215
|
-
<th>性別</th>
|
216
|
-
|
217
|
-
<th>名前</th>
|
218
|
-
|
219
|
-
<th>点数</th>
|
220
|
-
|
221
|
-
</tr>
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
{% for Object in object_list %}
|
226
|
-
|
227
|
-
<tr>
|
228
|
-
|
229
|
-
<td>{{Object.BelongClass.ClassNumber}}</td>
|
230
|
-
|
231
|
-
<td>{{Object.BelongClass.HomeroomTeacher}}</td>
|
232
|
-
|
233
|
-
<td>{{Object.Number}}</td>
|
234
|
-
|
235
|
-
<td>{{Object.get_ForM_display}}</td>
|
236
|
-
|
237
|
-
<td>{{Object.Name}}</td>
|
238
|
-
|
239
|
-
<td>{{Object.Point}}</td>
|
240
|
-
|
241
|
-
</tr>
|
242
|
-
|
243
|
-
{% endfor %}
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
</table>
|
248
|
-
|
249
|
-
```
|