teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

views.py 追記しました。

2019/01/26 15:13

投稿

atsuton
atsuton

スコア12

title CHANGED
File without changes
body CHANGED
@@ -86,6 +86,13 @@
86
86
  def chapter_and_section_list(request, pk):
87
87
  chapter = get_object_or_404(Chapter, pk=pk)
88
88
  return render(request, 'anki/chapter_and_section_list.html', {'chapter': chapter})
89
+
90
+ # 2019.1.27追加
91
+ from django.db.models import Q
92
+ def study_status_by_user(request, pk):
93
+ status = SectionAndContent.objects.filter(Q(studystatusbyuser__user=request.user) | Q(studystatusbyuser__isnull=True)).values(
94
+ 'studystatusbyuser__check_status', 'studystatusbyuser__comment', 'section_title')
95
+ return render(request, 'anki/study_status_by_user.html', {'status':status})
89
96
  ```
90
97
 
91
98
 

2

StudyStatusByUser の項目を追加しました。

2019/01/26 15:13

投稿

atsuton
atsuton

スコア12

title CHANGED
File without changes
body CHANGED
@@ -61,11 +61,18 @@
61
61
  def my_function(self):
62
62
  return str(self.content_text).splitlines()
63
63
 
64
-
64
+ # 2019.1.27修正 ※習熟度追加
65
65
  class StudyStatusByUser(models.Model):
66
+ PROFICIENCY_CHOICES = (
67
+ (0, ' '),
68
+ (1, '1'),
69
+ (2, '2'),
70
+ (3, '3'),
71
+ )
66
72
  user = models.ForeignKey('users.User', on_delete=models.CASCADE)
67
73
  section = models.ForeignKey(SectionAndContent, on_delete=models.CASCADE)
68
74
  check_status = models.BooleanField(default=False)
75
+ proficiency = models.IntegerField(verbose_name='習熟度', blank=True, null=True, default=0, choices=PROFICIENCY_CHOICES)
69
76
  comment = models.CharField(max_length=255,default='', blank=True)
70
77
 
71
78
  class Meta:

1

models.py・views.py・templateの内容を追加しました。少し長くなってしまいました。

2019/01/26 15:11

投稿

atsuton
atsuton

スコア12

title CHANGED
File without changes
body CHANGED
@@ -11,28 +11,26 @@
11
11
  その場合、ユーザーに紐づいた model を作って実装すると思われるのですが、
12
12
  やり方がわからないため質問させていただきました。
13
13
 
14
- 具体的なイメージは下です。
14
+ H31.1.20 修正・追
15
- ●登録画面
15
+ ユーザーについては、users というアプリを作成しました。
16
- □ チャター1 「コメント入力欄」
16
+ 暗記アリは anki というアプリを作成しています。
17
- □ チャプター2 「コメント入力欄」
17
+ anki の中の models.py に users と紐づけた
18
- □ ャプター3 「コメント入力欄」
18
+ StudyStatusByUser というmodel を追加し、ェックの状況やコメントを管理しようとしています。
19
19
 
20
- [更新]
20
+ 最終的には、ユーザー側でのチェックボックス操作・コメント追加 をしたいのですが、
21
+ それ以前に SectionAndContent と StudyStatusByUser のデータを紐づけて、
22
+ 一覧の表示することができないので、その点の解決方法(データの取得方法など)を教えていただけますとありがたいです。
21
23
 
22
- ===
23
- ※「チャプターX」部分はユーザーは閲覧
24
+ SQLだと、下記ようになるかと思います
25
+ ```sql
26
+ SELECT section_title, check_status, comment From SectionAndContent AS SAC Left Outer Join
27
+ StudyStatusByUser AS SSBU ON SSBU.section.id = SAC.id WHERE SSBU.user = request.user
28
+ ```
24
29
 
25
- 「□」はチェックボックスです。
26
-
27
- ●閲覧画面
28
- ☑ チャプター1 このチャプターの注意事項は〷。
29
- □ チャプター2
30
- □ チャプター3
31
-
32
-
33
30
  ### 該当のソースコード
34
31
 
35
32
  ```python
33
+ models.py
36
34
  class Book(models.Model):
37
35
  book_title = models.CharField('タイトル', max_length=100)
38
36
  def __str__(self):
@@ -45,10 +43,71 @@
45
43
  chapter_title = models.CharField('タイトル', max_length=100)
46
44
  def __str__(self):
47
45
  return str(self.chapter_number) + ". " + self.chapter_title
46
+
47
+
48
+ H31.1.20 追記
49
+ class SectionAndContent(models.Model):
50
+ RANK_CHOICES = (('A','A'),('B','B'),('C','C'),(' ',' '))
51
+ chapter = models.ForeignKey(Chapter, on_delete=models.PROTECT, to_field="chapter_number")
52
+ section_number = models.IntegerField(verbose_name='節番号', validators=[MinValueValidator(1), MaxValueValidator(100)])
53
+ section_title = models.CharField('タイトル', max_length=100)
54
+ display_order = models.IntegerField(verbose_name='表示順', validators=[MinValueValidator(1), MaxValueValidator(100)])
55
+ rank = models.CharField(verbose_name='ランク', max_length=2, choices=RANK_CHOICES, default=' ')
56
+ content_text = models.TextField(verbose_name='', blank=True, null=True, max_length=5000)
57
+
58
+ def __str__(self):
59
+ return str(self.chapter.id) + "-" + str(self.section_number) + ". " + self.section_title
60
+
61
+ def my_function(self):
62
+ return str(self.content_text).splitlines()
63
+
64
+
65
+ class StudyStatusByUser(models.Model):
66
+ user = models.ForeignKey('users.User', on_delete=models.CASCADE)
67
+ section = models.ForeignKey(SectionAndContent, on_delete=models.CASCADE)
68
+ check_status = models.BooleanField(default=False)
69
+ comment = models.CharField(max_length=255,default='', blank=True)
70
+
71
+ class Meta:
72
+ unique_together = ('user', 'section')
73
+
74
+ def __str__(self):
75
+ return str(self.section)
76
+
77
+
78
+ views.py ※一部
79
+ def chapter_and_section_list(request, pk):
80
+ chapter = get_object_or_404(Chapter, pk=pk)
81
+ return render(request, 'anki/chapter_and_section_list.html', {'chapter': chapter})
48
82
  ```
49
83
 
50
84
 
85
+ 下記テンプレートの中に、StudyStatusByUser の check_status と comment を紐づけたいのですが、
86
+ "chapter.sectionandcontent_set.all"
87
+
88
+ "chapter.sectionandcontent_set.all.filter(StudyStatusByUser__user=request.user)"
89
+ に変更すると、エラー(Could not parse the remainder)になりました。
51
90
 
91
+
92
+ ```html
93
+ chapter_and_section_list.html
94
+ {% block content %}
95
+ <div class="chapter-title">
96
+ <h2>{{ chapter }}</h2>
97
+ </div>
98
+ {% for chapter in chapter.sectionandcontent_set.all %}
99
+ <div class="section-title">
100
+ <h1><a href="{% url 'section_and_content' pk=chapter.pk %}">
101
+ {{ chapter.chapter.id }}-{{ chapter.section_number }}.{{ chapter.section_title }}
102
+ </a></h1>
103
+ </div>
104
+ {% endfor %}
105
+ {% endblock %}
106
+
107
+ ```
108
+
109
+
110
+
52
111
  ### 補足情報
53
112
 
54
113
  ・python 3.7.2 を使用しています。