質問編集履歴

3

appの一部を統合

2023/03/27 12:33

投稿

Te_PiN
Te_PiN

スコア20

test CHANGED
File without changes
test CHANGED
@@ -13,18 +13,17 @@
13
13
  Corp(1)対 Staff(多)
14
14
 
15
15
  Djangoアプリケーションを
16
- 「accounts」「corp」「staff」に分けて作成しています。
16
+ 「accounts」「corp」に分けて作成しています。
17
17
 
18
- app(プロジェクト)
18
+ app
19
19
  |-accounts
20
20
  |-corp
21
- |-staff
22
21
  |-app
23
22
 
24
23
  ### 発生している問題・エラーメッセージ
25
24
  staffを参照するリンク(staff_list)を叩くと以下のエラーが発生します。
26
25
 
27
- AttributeError at /corp/staff_list/
26
+ AttributeError at /corp/1/staff-list/
28
27
  'WSGIRequest' object has no attribute 'corp'
29
28
 
30
29
  ### 該当のソースコード
@@ -64,12 +63,6 @@
64
63
 
65
64
  def __str__(self):
66
65
  return self.corp_name
67
- ```
68
- ---
69
- staff/models.py
70
- ```python
71
- from django.db import models
72
- from corp.models import Corp
73
66
 
74
67
 
75
68
  class Staff(models.Model):
@@ -95,20 +88,45 @@
95
88
  ```
96
89
  ---
97
90
 
98
- 追記)staff/views.py
91
+ 追記)corp/ulrs.py
99
92
  ```python
93
+ #corp/ulrs.py
94
+ # (省略)
95
+ path('corp-list/', views.CorpListView.as_view(), name="corp_list"),
96
+ path('corp/<int:pk>/', views.CorpView.as_view(), name="corp"),
97
+
98
+ path('corp/<int:pk>/staff-list/', views.StaffListView.as_view(), name='staff_list'),
99
+ ```
100
+
101
+ 追記)corp/views.py
102
+ ```python
100
- from corp.models import Corp
103
+ from .models import Corp, Staff
101
104
  from django.views import generic
105
+
106
+ #CorpListViewは正常に取得できる
107
+ class CorpListView(generic.ListView):
102
- from .models import Staff
108
+ model = Corp
109
+ template_name = 'corp_list.html'
110
+ paginate_by = 5
111
+
112
+ def get_queryset(self):
113
+ corps = Corp.objects.filter(user=self.request.user).order_by('corp_name')
114
+ return corps
115
+
116
+ #CorpViewも正常に表示できる
117
+ class CorpView(generic.DetailView):
118
+ model = Corp
119
+ template_name = 'corp.html'
103
120
 
104
121
 
122
+ #StaffListViewはエラーになってしまう
105
123
  class StaffListView(generic.ListView):
124
+ model = Staff
106
125
  template_name = "staff_list.html"
126
+ paginate_by = 10
107
127
 
108
128
  def get_queryset(self):
109
-
110
- staff_list = Staff.objects.filter(corp=self.request.corp).order_by('staff_name')
129
+ staff_list = Staff.objects.filter(corp=self.request.corp).order_by('last_name')
111
-
112
130
  return staff_list
113
131
  ```
114
132
  ---
@@ -121,8 +139,8 @@
121
139
 
122
140
 
123
141
  ### 試したこと
124
-
142
+ マイグレーションをやり直したりデータベースの確認、
125
- 色々と調べましたが原因が分からず
143
+ インターネットで同様の解説を検索など
126
144
 
127
145
  ### 補足情報(FW/ツールのバージョンなど)
128
146
 

2

エラー表示修正

2023/03/20 05:31

投稿

Te_PiN
Te_PiN

スコア20

test CHANGED
File without changes
test CHANGED
@@ -25,7 +25,7 @@
25
25
  staffを参照するリンク(staff_list)を叩くと以下のエラーが発生します。
26
26
 
27
27
  AttributeError at /corp/staff_list/
28
- 'CustomUser' object has no attribute 'corp'
28
+ 'WSGIRequest' object has no attribute 'corp'
29
29
 
30
30
  ### 該当のソースコード
31
31
  accounts/models.py

1

staff/views.py 追記

2023/03/20 05:29

投稿

Te_PiN
Te_PiN

スコア20

test CHANGED
File without changes
test CHANGED
@@ -94,11 +94,32 @@
94
94
 
95
95
  ```
96
96
  ---
97
+
98
+ 追記)staff/views.py
99
+ ```python
100
+ from corp.models import Corp
101
+ from django.views import generic
102
+ from .models import Staff
103
+
104
+
105
+ class StaffListView(generic.ListView):
106
+ template_name = "staff_list.html"
107
+
108
+ def get_queryset(self):
109
+
110
+ staff_list = Staff.objects.filter(corp=self.request.corp).order_by('staff_name')
111
+
112
+ return staff_list
113
+ ```
114
+ ---
115
+ staff/templates/staff_list.html
97
116
  ```html
98
117
  <!-- 省略 -->
99
118
  <a href="{% url 'staff:staff_list' %}">従業員情報</a>
100
119
  <!-- 省略 -->
101
120
  ```
121
+
122
+
102
123
  ### 試したこと
103
124
 
104
125
  色々と調べましたが原因が分からず