質問編集履歴

3

modelの内容を変更

2021/08/25 03:04

投稿

art35
art35

スコア13

test CHANGED
File without changes
test CHANGED
@@ -4,20 +4,82 @@
4
4
 
5
5
  calibration_dateをtodayなどで入力してその何年後かをnext_calibration_dateに表示させる。
6
6
 
7
- calibration_typeによってCALIBRATION_CHOICESから参照して次の指定日をreadonlyで表示させる。
7
+ 選択したcalibration_typeによってCALIBRATION_CHOICESから参照して次の指定日を自動で入力。管理画面でreadonlyで表示させる。
8
8
 
9
9
  readonly→これはadminでできているので多分いいと思ってます。
10
10
 
11
11
 
12
12
 
13
- runserverを動かして登録しようとすると以下のようなメッセージが出る。
13
+
14
-
15
-
16
-
17
- models.pyの
18
14
 
19
15
  ```python
20
16
 
17
+ _models.py
18
+
19
+
20
+
21
+
22
+
23
+ CALIBRATION_CHOICES = (
24
+
25
+ (0,'1年校正'),
26
+
27
+ (1,'2年校正'),
28
+
29
+ (2,'5年校正'),
30
+
31
+ (3,'10年校正'),
32
+
33
+ )
34
+
35
+
36
+
37
+ SPEC3_CHOICES = (
38
+
39
+ (1, 'A'),
40
+
41
+ (2, 'D'),
42
+
43
+ )
44
+
45
+
46
+
47
+
48
+
49
+ class InsLedger(models.Model):
50
+
51
+ measuring_ins_group = models.IntegerField(choices=MEASURING_INS_GROUP_CHOICES,default=1)
52
+
53
+ measuring_tool = models.ForeignKey(Device, on_delete=models.CASCADE)
54
+
55
+ ins_num = models.CharField(max_length=10)
56
+
57
+ calibration_type = models.IntegerField(choices=CALIBRATION_CHOICES)#校正期間
58
+
59
+ calibration_date = models.DateField(default=now)#datetime.datetime.now())
60
+
61
+ next_calibration_date = models.DateField()
62
+
63
+ serial_num = models.CharField(max_length=20, null=True, blank=True, default='')
64
+
65
+ product_num = models.ForeignKey(Product_num, on_delete=models.CASCADE)#対象品番
66
+
67
+ team = models.ForeignKey(TeamModel, on_delete=models.CASCADE, related_name='teams1')
68
+
69
+ line = models.ForeignKey(LineModel, on_delete=models.CASCADE, related_name='lines1')
70
+
71
+ useapp = models.ForeignKey(Useapp, on_delete=models.CASCADE)#使用用途
72
+
73
+ spec1 = models.ForeignKey(Spec1, on_delete=models.CASCADE)#最小目盛
74
+
75
+ spec2 = models.ForeignKey(Spec2, on_delete=models.CASCADE)#測定範囲
76
+
77
+ spec3 = models.IntegerField(choices=SPEC3_CHOICES)#表示
78
+
79
+ check_items = models.ForeignKey(Check_Items, on_delete=models.CASCADE)#日常点検項目
80
+
81
+
82
+
21
83
  def save(self, *args, **kwargs):
22
84
 
23
85
  auto_now = kwargs.pop('next_calibration_date_auto_now', True)
@@ -42,17 +104,55 @@
42
104
 
43
105
 
44
106
 
45
- **self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
107
+ self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
46
-
47
-
48
-
108
+
109
+
110
+
49
- super().save(*args, **kwargs)**
111
+ super().save(*args, **kwargs)
112
+
113
+
114
+
50
-
115
+ def __str__(self):
116
+
117
+ return str(self.measuring_tool)
118
+
51
- ```
119
+ ```
120
+
121
+
122
+
52
-
123
+ ```python
124
+
125
+ _admin.py
126
+
127
+
128
+
129
+ class InsLedgerAdmin(admin.ModelAdmin):
130
+
131
+ list_display = ('measuring_ins_group','measuring_tool','ins_num','team','line','calibration_date', 'next_calibration_date')
132
+
133
+ list_filter = ["measuring_ins_group"]
134
+
135
+ search_fields = ['measuring_ins_group']
136
+
137
+ readonly_fields = ('next_calibration_date',)
138
+
53
- この部分を段落を下げたらエラーは出なくなったが、登録してもIDがないため消去したといわれる。
139
+ admin.site.register(InsLedger, InsLedgerAdmin)
140
+
141
+
142
+
54
-
143
+ ```
144
+
145
+
146
+
147
+
148
+
149
+
150
+
55
- なぜなでしょ
151
+ runserverを動かして登録したところ以下なメッセージが出る
152
+
153
+
154
+
155
+
56
156
 
57
157
 
58
158
 
@@ -76,127 +176,15 @@
76
176
 
77
177
  local variable 'calibration_span' referenced before assignment
78
178
 
79
- Exception Location: ―--/ins_ledger1/models.py, line 71, in save
179
+ Exception Location: ―--/ins_ledger1/models.py, line 72, in save
80
-
81
-
82
-
180
+
181
+
182
+
83
- 71 self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
183
+ 72 self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
84
-
184
+
85
- ```
185
+ ```
86
-
87
-
88
-
89
- ### 該当のソースコード
186
+
90
-
91
-
92
-
93
- ```python
187
+
94
-
95
- -models.py
96
-
97
-
98
-
99
- CALIBRATION_CHOICES = (
100
-
101
- (0,'1年校正'),
102
-
103
- (1,'2年校正'),
104
-
105
- (2,'5年校正'),
106
-
107
- (3,'10年校正'),
108
-
109
- )
110
-
111
-
112
-
113
- class InsLedger(models.Model):
114
-
115
- measuring_ins_group = models.IntegerField(choices=MEASURING_INS_GROUP_CHOICES,default=1)
116
-
117
- measuring_tool = models.ForeignKey(Device, on_delete=models.CASCADE)
118
-
119
- ins_num = models.CharField(max_length=10)
120
-
121
- calibration_type = models.IntegerField(default=0,choices=CALIBRATION_CHOICES)#校正期間
122
-
123
- calibration_date = models.DateTimeField(default=now)#datetime.datetime.now())
124
-
125
- next_calibration_date = models.DateTimeField()
126
-
127
- serial_num = models.CharField(max_length=20, null=True, blank=True, default='')
128
-
129
- product_num = models.ForeignKey(Product_num, on_delete=models.CASCADE)#対象品番
130
-
131
- team = models.ForeignKey(TeamModel, on_delete=models.CASCADE, related_name='teams1')
132
-
133
- line = models.ForeignKey(LineModel, on_delete=models.CASCADE, related_name='lines1')
134
-
135
- useapp = models.ForeignKey(Useapp, on_delete=models.CASCADE)#使用用途
136
-
137
- spec1 = models.ForeignKey(Spec1, on_delete=models.CASCADE)#最小目盛
138
-
139
- spec2 = models.ForeignKey(Spec2, on_delete=models.CASCADE)#測定範囲
140
-
141
- spec3 = models.IntegerField(choices=SPEC3_CHOICES)#表示
142
-
143
- check_items = models.ForeignKey(Check_Items, on_delete=models.CASCADE)#日常点検項目
144
-
145
-
146
-
147
- def save(self, *args, **kwargs):
148
-
149
- auto_now = kwargs.pop('next_calibration_date_auto_now', True)
150
-
151
- if auto_now:
152
-
153
- if self.calibration_type == CALIBRATION_CHOICES[0]:
154
-
155
- calibration_span = 1
156
-
157
- elif self.calibration_type == CALIBRATION_CHOICES[1]:
158
-
159
- calibration_span = 2
160
-
161
- elif self.calibration_type == CALIBRATION_CHOICES[2]:
162
-
163
- calibration_span = 5
164
-
165
- elif self.calibration_type == CALIBRATION_CHOICES[3]:
166
-
167
- calibration_span = 10
168
-
169
- self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
170
-
171
- super().save(*args, **kwargs)
172
-
173
-
174
-
175
- def __str__(self):
176
-
177
- return str(self.measuring_tool)
178
-
179
- ```
180
-
181
- ```python
182
-
183
- -admin.py
184
-
185
-
186
-
187
- class InsLedgerAdmin(admin.ModelAdmin):
188
-
189
- list_display = ('measuring_ins_group','measuring_tool','ins_num','team','line','calibration_date', 'next_calibration_date')
190
-
191
- list_filter = ["measuring_ins_group"]
192
-
193
- search_fields = ['measuring_ins_group']
194
-
195
- readonly_fields = ('next_calibration_date',)
196
-
197
- admin.site.register(InsLedger, InsLedgerAdmin)
198
-
199
- ```
200
188
 
201
189
  ### 試したこと
202
190
 
@@ -210,4 +198,4 @@
210
198
 
211
199
 
212
200
 
213
- ここにより詳細情報を記載てください
201
+ たかわかる方ご教授お願いいたます

2

質問を変更しました。

2021/08/25 03:04

投稿

art35
art35

スコア13

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,48 @@
14
14
 
15
15
 
16
16
 
17
+ models.pyの
18
+
19
+ ```python
20
+
21
+ def save(self, *args, **kwargs):
22
+
23
+ auto_now = kwargs.pop('next_calibration_date_auto_now', True)
24
+
25
+ if auto_now:
26
+
27
+ if self.calibration_type == CALIBRATION_CHOICES[0]:
28
+
29
+ calibration_span = 1
30
+
31
+ elif self.calibration_type == CALIBRATION_CHOICES[1]:
32
+
33
+ calibration_span = 2
34
+
35
+ elif self.calibration_type == CALIBRATION_CHOICES[2]:
36
+
37
+ calibration_span = 5
38
+
39
+ elif self.calibration_type == CALIBRATION_CHOICES[3]:
40
+
41
+ calibration_span = 10
42
+
43
+
44
+
45
+ **self.next_calibration_date = self.calibration_date + relativedelta(years=calibration_span)
46
+
47
+
48
+
49
+ super().save(*args, **kwargs)**
50
+
51
+ ```
52
+
53
+ この部分を段落を下げたらエラーは出なくなったが、登録してもIDがないため消去したといわれる。
54
+
55
+ なぜなのでしょうか。
56
+
57
+
58
+
17
59
  ### 発生している問題・エラーメッセージ
18
60
 
19
61
 

1

不必要な部分を削除

2021/08/24 08:32

投稿

art35
art35

スコア13

test CHANGED
File without changes
test CHANGED
@@ -25,10 +25,6 @@
25
25
  local variable 'calibration_span' referenced before assignment
26
26
 
27
27
 
28
-
29
- Request Method: POST
30
-
31
- Request URL: http://localhost:8000/admin/ins_ledger1/insledger/add/?_changelist_filters=measuring_ins_group__exact%3D1
32
28
 
33
29
  Django Version: 3.2.5
34
30