In my django app I have a "Incidence" model that has several FileField and ImageField. I want to create an endpoint with Django Rest Framework to retrieve the information about an "Incidence" but what I have done so far only retrieves the url of the files. I need to return the files in a format that can be downloaded by the client application like binary data or other suggestion. How can I do it? This is what I have done so far
Model Definition:
class Incidence(BasicAuditModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
geom = models.PointField(verbose_name=_("Localización"), srid=4326, blank=True, null=True)
name = models.CharField(_("Nombre"), max_length=255)
incidence_type = models.CharField(_("Tipo incidente"), max_length=50, choices=INCIDENCE_TYPE_CHOICES, default="tipo1")
description = models.TextField(_("Descripción"), blank=True, null=True)
status = models.CharField(_("Status"), max_length=50, choices=STATUS_CHOICES, default="creado")
image1 = models.ImageField(verbose_name=_("Foto 1"), upload_to="incidencia_fotos",
null=False, blank=False)
image2 = models.ImageField(verbose_name=_("Foto 2"), upload_to="incidencia_fotos",
null=False, blank=False)
image3 = models.ImageField(verbose_name=_("Foto 3"), upload_to="incidencia_fotos",
null=False, blank=False)
audio = models.FileField(verbose_name=_("Audio"), upload_to="incidencia_audioa",
null=False, blank=False)
video = models.FileField(verbose_name=_("Video"), upload_to="incidencia_video",
null=False, blank=False)
def __str__(self):
return self.name
class Meta:
db_table = 'sini_incidence'
managed = True
verbose_name = 'Incidencia'
verbose_name_plural = 'Incidencias'
class BasicAuditModel(models.Model):
created_by = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name="+",
verbose_name=_("Creado por"),
null=True,
blank=False,
on_delete=models.SET_NULL)
modified_by = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name="+",
verbose_name=_("Modificado por"),
null=True,
blank=False,
on_delete=models.SET_NULL)
created = models.DateTimeField(auto_now_add=True, verbose_name=_("Fecha creado"))
modified = models.DateTimeField(auto_now=True, verbose_name=_("Fecha modificado"))
Serializer class:
from rest_framework_gis.serializers import GeoFeatureModelSerializer
class IncidenceSerializer(GeoFeatureModelSerializer):
class Meta:
model = Incidence
geo_field = "geom"
fields = ('id','geom','name','incidence_type',
'description','status', 'image1', 'image2', 'image3', 'audio', 'video')
extra_kwargs = {'geom': {'required': True}}
read_only_fields = ['id','created_by', 'modified_by', 'created','modified']
This is the View:
class IncidenceAPIRetrieve(generics.RetrieveAPIView):
authentication_classes = [JWTAuthentication]
permission_classes = (IsAuthenticated,)
queryset = Incidence.objects.all()
serializer_class = IncidenceSerializer
This is the JSON i am getting. As you can see the endpoint returns the url to the files. I need to be able to download the files.