質問編集履歴
5
改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
|
23
23
|
### 該当のソースコード
|
24
24
|
|
25
|
-
```views
|
25
|
+
```views
|
26
26
|
|
27
27
|
class BlogList(LoginRequiredMixin, TemplateView):
|
28
28
|
|
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
```models
|
55
|
+
```models
|
56
56
|
|
57
57
|
class Status(models.Model):
|
58
58
|
|
@@ -114,7 +114,7 @@
|
|
114
114
|
|
115
115
|
### 試したこと
|
116
116
|
|
117
|
-
```views
|
117
|
+
```views
|
118
118
|
|
119
119
|
context['all_posts'] = Post.objects.filter(status=member.status.all())
|
120
120
|
|
@@ -122,7 +122,7 @@
|
|
122
122
|
|
123
123
|
ではこの段階では上手くいくが、
|
124
124
|
|
125
|
-
```list
|
125
|
+
```list
|
126
126
|
|
127
127
|
{% for post in all_posts %}
|
128
128
|
|
@@ -141,3 +141,95 @@
|
|
141
141
|
### 補足情報
|
142
142
|
|
143
143
|
python3.7.2
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
### さらなる情報追加
|
148
|
+
|
149
|
+
```list
|
150
|
+
|
151
|
+
<!DOCTYPE html>
|
152
|
+
|
153
|
+
<html>
|
154
|
+
|
155
|
+
<head>
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
</head>
|
160
|
+
|
161
|
+
<body>
|
162
|
+
|
163
|
+
{% for member_status in member.status.all %}
|
164
|
+
|
165
|
+
<p>{{ member_status }}</p>
|
166
|
+
|
167
|
+
{% endfor %}
|
168
|
+
|
169
|
+
{% for post in all_posts %}
|
170
|
+
|
171
|
+
<div><a href="{% url 'blog:index' post.id %}">{{ post.summary }}</a></div>
|
172
|
+
|
173
|
+
<p>{{ post.status }}</p>
|
174
|
+
|
175
|
+
{% endfor %}
|
176
|
+
|
177
|
+
</body>
|
178
|
+
|
179
|
+
</html>
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
```views
|
184
|
+
|
185
|
+
def get_context_data(self, **kwargs):
|
186
|
+
|
187
|
+
context = super().get_context_data(**kwargs)
|
188
|
+
|
189
|
+
member = Member.objects.get(user=self.request.user)
|
190
|
+
|
191
|
+
context['member'] = member
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
status_list = ['Vlog', 'Beginner']
|
196
|
+
|
197
|
+
context['all_posts'] = Post.objects.filter(status=status_list)
|
198
|
+
|
199
|
+
#print(Post.objects.all().values())
|
200
|
+
|
201
|
+
return context
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
上記の場合のエラー画面
|
206
|
+
|
207
|
+
![上記の場合のエラー画面](0fbef62dc45f8da6d1754db24e8ba432.png)
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
```views
|
212
|
+
|
213
|
+
def get_context_data(self, **kwargs):
|
214
|
+
|
215
|
+
context = super().get_context_data(**kwargs)
|
216
|
+
|
217
|
+
member = Member.objects.get(user=self.request.user)
|
218
|
+
|
219
|
+
context['member'] = member
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
#status_list = ['Vlog', 'Beginner']
|
224
|
+
|
225
|
+
#context['all_posts'] = Post.objects.filter(status=status_list)
|
226
|
+
|
227
|
+
print(Post.objects.all().values())
|
228
|
+
|
229
|
+
return context
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
上記の場合の実行結果
|
234
|
+
|
235
|
+
![上記の場合の実行結果](e5789a90188d6c101701f10c51977372.png)
|
4
改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Django:
|
1
|
+
Django: データベース操作
|
test
CHANGED
File without changes
|
3
改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,13 +116,27 @@
|
|
116
116
|
|
117
117
|
```views.py
|
118
118
|
|
119
|
-
objects.filter
|
119
|
+
context['all_posts'] = Post.objects.filter(status=member.status.all())
|
120
|
-
|
121
|
-
objects.get(status='Vlog')
|
122
120
|
|
123
121
|
```
|
124
122
|
|
123
|
+
ではこの段階では上手くいくが、
|
124
|
+
|
125
|
+
```list.html
|
126
|
+
|
127
|
+
{% for post in all_posts %}
|
128
|
+
|
129
|
+
{% endfor %}
|
130
|
+
|
131
|
+
```
|
132
|
+
|
125
|
-
|
133
|
+
で以下のエラーが発生してしまう。
|
134
|
+
|
135
|
+
```error
|
136
|
+
|
137
|
+
The QuerySet value for an exact lookup must be limited to one result using slicing.
|
138
|
+
|
139
|
+
```
|
126
140
|
|
127
141
|
### 補足情報
|
128
142
|
|
2
改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
statusがMember(ログイン時のユーザ)が保持しているstatus(複数の可能性あり)である記事を取り出したいと思っています。
|
4
4
|
|
5
|
+
しかし、下記エラーが発生してしまいます。
|
6
|
+
|
5
|
-
|
7
|
+
どのようにすれば解決出来るのか教えて頂けると幸いです。
|
8
|
+
|
9
|
+
可能性のみでもお願いします。
|
6
10
|
|
7
11
|
よろしくお願いいたします。
|
8
12
|
|
@@ -22,19 +26,25 @@
|
|
22
26
|
|
23
27
|
class BlogList(LoginRequiredMixin, TemplateView):
|
24
28
|
|
25
|
-
model =
|
29
|
+
model = Member
|
26
30
|
|
27
31
|
template_name = 'blog/list.html'
|
28
32
|
|
29
|
-
|
33
|
+
|
30
34
|
|
31
35
|
def get_context_data(self, **kwargs):
|
32
36
|
|
33
37
|
context = super().get_context_data(**kwargs)
|
34
38
|
|
35
|
-
|
39
|
+
member = Member.objects.get(user=self.request.user)
|
36
40
|
|
41
|
+
context['member'] = member
|
42
|
+
|
43
|
+
|
44
|
+
|
37
|
-
context['all_post'] = Post.objects.filter(status=
|
45
|
+
context['all_posts'] = Post.objects.filter(status=member.status)
|
46
|
+
|
47
|
+
|
38
48
|
|
39
49
|
return context
|
40
50
|
|
@@ -82,6 +92,24 @@
|
|
82
92
|
|
83
93
|
return markdownify(self.contents)
|
84
94
|
|
95
|
+
|
96
|
+
|
97
|
+
class Member(models.Model):
|
98
|
+
|
99
|
+
name = models.CharField(max_length=140)
|
100
|
+
|
101
|
+
mail = models.EmailField(max_length=140)
|
102
|
+
|
103
|
+
status = models.ManyToManyField(Status)
|
104
|
+
|
105
|
+
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def __str__(self):
|
110
|
+
|
111
|
+
return self.name
|
112
|
+
|
85
113
|
```
|
86
114
|
|
87
115
|
### 試したこと
|
1
改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -31,8 +31,6 @@
|
|
31
31
|
def get_context_data(self, **kwargs):
|
32
32
|
|
33
33
|
context = super().get_context_data(**kwargs)
|
34
|
-
|
35
|
-
user_info = Member.objects.get(user=self.request.user)
|
36
34
|
|
37
35
|
#context['all_post'] = Post.objects.get(status='Beginner')
|
38
36
|
|