30 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
| from apps.utils.serializers import CustomModelSerializer
 | |
| from apps.em.models import Equipment, EcheckRecord
 | |
| from apps.system.models import Dept
 | |
| from apps.utils.constants import EXCLUDE_FIELDS, EXCLUDE_FIELDS_BASE
 | |
| from rest_framework import serializers
 | |
| from rest_framework.exceptions import ValidationError
 | |
| 
 | |
| class EquipmentSerializer(CustomModelSerializer):
 | |
|     belong_dept = serializers.PrimaryKeyRelatedField(label='责任部门', queryset = Dept.objects.all())
 | |
|     keeper_name = serializers.CharField(source='keeper.name', read_only=True)
 | |
|     belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
 | |
|     class Meta:
 | |
|         model = Equipment
 | |
|         fields = '__all__'
 | |
|         read_only_fields = EXCLUDE_FIELDS + ['check_date', 'next_check_date', 'keeper_name', 'belong_dept_name']
 | |
| 
 | |
| 
 | |
| class EcheckRecordSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = EcheckRecord
 | |
|         fields = '__all__'
 | |
|         read_only_fields = EXCLUDE_FIELDS
 | |
| 
 | |
|     def validate(self, attrs):
 | |
|         attrs = super().validate(attrs)
 | |
|         check_date = attrs['check_date']
 | |
|         if EcheckRecord.objects.filter(check_date__gte=check_date, equipment=attrs['equipment']).exists():
 | |
|             raise ValidationError('检定日期小于历史记录')
 | |
| 
 |