43 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			2.1 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
 | |
| 
 | |
| 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 + ['belong_dept']
 | |
|         extra_kwargs = {'val_avg': {'required': True, 'allow_null': False}, 'num_test':{'required': True, 'allow_null': False}, 'num_ok': {'required': True, 'allow_null': False}}
 | |
|     
 | |
|     def validate(self, attrs):
 | |
|         attrs['rate_pass'] = attrs['num_ok']/attrs['num_test']
 | |
|         attrs['belong_dept'] = attrs['sflog'].mgroup.belong_dept
 | |
|         return attrs
 | |
|     
 | |
| class QuaStatUpdateSerializer(CustomModelSerializer):
 | |
|     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 + ['belong_dept', 'sflog', 'material', 'testitem']
 | |
|         extra_kwargs = {'val_avg': {'required': True, 'allow_null': False}, 'num_test':{'required': True, 'allow_null': False}, 'num_ok': {'required': True, 'allow_null': False}}
 | |
|     
 | |
|     def validate(self, attrs):
 | |
|         attrs['rate_pass'] = attrs['num_ok']/attrs['num_test']
 | |
|         return super().validate(attrs) |