Попробовал через сериалайзер - вроде все работает
class DirectoryItemSerializer(serializers.ModelSerializer):
def create(self, validated_data):
directoryId = validated_data['directory'].id
isActive = True
code = validated_data['code']
directoryItems = DirectoryItem.objects.filter(directory=directoryId, is_active=isActive, code=code)
if directoryItems.count() != 0:
item = directoryItems[0]
if item.name == validated_data['name'] and item.short_name == validated_data['short_name']:
raise EntityExistException()
else:
item.is_active = False
item.save()
return DirectoryItem.objects.create(**validated_data)
else:
return DirectoryItem.objects.create(**validated_data)
class Meta:
model = DirectoryItem
fields = '__all__'