質問編集履歴

1

sdf

2020/11/17 08:29

投稿

_ecs
_ecs

スコア9

test CHANGED
File without changes
test CHANGED
@@ -32,29 +32,65 @@
32
32
 
33
33
  ```python
34
34
 
35
- #モデル
35
+ #シリアライザ
36
36
 
37
- class Command(models.Model):
37
+ class CommandSerializer(serializers.ModelSerializer):
38
38
 
39
- sender = models.IntegerField()
40
39
 
41
- cmd = models.TextField()
42
40
 
43
- recipient = models.TextField()
41
+ upfile = UpFileSerializer(read_only=True)
44
42
 
45
- response = models.TextField()
43
+ downfile = DownFileSerializer(read_only=True)
46
44
 
47
- upfile = models.OneToOneField(UpFile, default=None, null=True, blank=True, unique=True, related_name='command', on_delete=models.CASCADE)
48
45
 
49
- downfile = models.OneToOneField(DownFile, default=None, null=True, blank=True, unique=True, related_name='command', on_delete=models.CASCADE)
50
46
 
51
- cmd_finished = models.DateTimeField(null=True, blank=True)
47
+ class Meta:
52
48
 
49
+ model = Command
50
+
51
+ fields = '__all__'
52
+
53
+ extra_kwargs = {
54
+
55
+ 'response': {'required': False}
56
+
57
+ }
58
+
59
+
60
+
61
+
62
+
63
+ def create(self, validated_data):
64
+
65
+ c = Command(
66
+
67
+ sender=validated_data['sender'],
68
+
69
+ cmd=validated_data['cmd'],
70
+
71
+ recipient=validated_data['recipient']
72
+
73
+ )
74
+
75
+ c.finished('cmd')
76
+
77
+ return c
78
+
79
+
80
+
81
+ def update(self, instance, validated_data):
82
+
53
- resp_finished = models.DateTimeField(null=True, blank=True)
83
+ instance.response = validated_data.get('response', instance.response)
84
+
85
+ instance.finished('resp')
86
+
87
+ return instance
54
88
 
55
89
  ```
56
90
 
57
91
  ```python
92
+
93
+ #モデル
58
94
 
59
95
  class Command(models.Model):
60
96