I am stuck to upload a multiple image file at duration of creating doctor model .. I think to implement multiple upload, you need to set the additional field for uploading in the DoctorSerializer.
I want to upload multiple images for each student using django rest framework. currently, Im only able to upload one image for each student, but I want to be able to upload multiple images for each doctor in uploads folder in a directory of their name. Im building a facial attendance recognition system and I need multiple images of each student. here is my models.py file.
Models.py
class Doctor(models.Model):
def get_upload_path(instance, filename):
return 'documents/{0}/{1}'.format(instance.name, filename)
mobile_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Up to 15 digits allowed.")
name = models.CharField(max_length=255, blank=False, null=False)
mobile = models.CharField(validators=[mobile_regex], max_length=15)
email = models.EmailField(max_length=255, blank=False, null=False)
picture = models.ImageField(upload_to="images/", blank=True, null=True)
document = models.FileField(upload_to=get_upload_path, null=True, blank=True)
and Here is my serializers.py
class DoctorSerializer(serializers.ModelSerializer):
class Meta:
model = Doctor
fields = ['id','name','email','mobile' ,'document',]
def create(self, validated_data):
return Doctor.objects.create(**validated_data)
and my views.py file...
class CreateDoctorAPIView(APIView):
parser_classes = (MultiPartParser, )
def post(self,request,*args):
try:
files = request.FILES['document']
print(files)
serializer = DoctorSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({
'message': 'Doctor created successfully',
})
else:
return Response({
'message':'something wrong'
})
except Exception as e:
return Response({
'mesaage':str(e)
})