33 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
| from apps.qm.models import QuaStat, TestItem
 | |
| from apps.utils.constants import EXCLUDE_FIELDS
 | |
| from apps.utils.serializers import CustomModelSerializer
 | |
| from rest_framework import serializers
 | |
| from apps.system.models import Dept, Dictionary
 | |
| from apps.wpm.models import SfLog
 | |
| from django.utils.timezone import localtime
 | |
| 
 | |
| class TestItemSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = TestItem
 | |
|         fields = '__all__'
 | |
|         read_only_fields = EXCLUDE_FIELDS
 | |
| 
 | |
| class QuaStatSerializer(CustomModelSerializer):
 | |
|     sflog = serializers.PrimaryKeyRelatedField(label="值班记录", queryset=SfLog.objects.all(), required=True)
 | |
|     belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
 | |
|     material_name = serializers.CharField(source='material.name', read_only=True)
 | |
|     testitem_name = serializers.CharField(source='testitem.name', read_only=True)
 | |
|     class Meta:
 | |
|         model = QuaStat
 | |
|         fields = '__all__'
 | |
|         read_only_fields = EXCLUDE_FIELDS + ['type', 'rate_pass', 'year_s', 'month_s', 'day_s', 'belong_dept']
 | |
|         extra_kwargs = {'val_avg': {'required': True}, 'num_test':{'required': True}, 'num_ok': {'required': True}}
 | |
|     
 | |
|     def validate(self, attrs):
 | |
|         attrs['type'] = 'sflog'
 | |
|         attrs['belong_dept'] = attrs['sflog'].belong_dept
 | |
|         end_time = attrs['sflog'].end_time
 | |
|         end_time_local = localtime(end_time)
 | |
|         attrs['year_s'], attrs['month_s'], attrs['day_s'] = end_time_local.year, end_time_local.month, end_time_local.day
 | |
|         attrs['rate_pass'] = attrs['num_ok']/attrs['num_test']
 | |
|         return attrs |