I have a validate_filename function in a serializer but seems like I have to use the same function inside multiple serializer classes to validate the file name. It doesn't seem good because it seems redundant and the concept of DRY is not being implemented.
For eg:
class ExampleOne(serializers.ModelSerializer):
class Meta:
model = ExampleOne
fields =['']
def create(self, validated_data): ...
return item
def validate_extension(filename):
extension = os.path.splitext(filename)[1].replace(".", "")
if extension.lower() not in ALLOWED_IMAGE_EXTENSIONS:
raise serializers.ValidationError(
(f'Invalid uploaded file type: {filename}'),
code='invalid',
Now, I have to use the same function inside other serializers class inside the same serializer file and don't want to repeat the same code again. Is there any way we can use a separate file like validatefile.py and import the function only inside the serializer class?? But dont really know how to start and go about it.