Home > Mobile >  how to upload all type of file to cloudinary using django with filefield
how to upload all type of file to cloudinary using django with filefield

Time:02-11

hello guys i want to upload all type of file to cloudinary this is my model i don't want to upload a specific kind of file but all type pleaze help me

class post(models.Model):
titre=models.CharField(unique=True,null=True,max_length=100)
description=models.TextField(null=True,blank=True,max_length=400)
T=models.CharField(default="image",blank=True,max_length=50)
image=models.FileField(null=True)
cat=models.ForeignKey(categorie,on_delete=models.CASCADE,null=True)
datepost=models.DateTimeField(auto_now_add=True,blank=True)
user=models.ForeignKey(myuser,on_delete=models.CASCADE,null=True)
vue=models.IntegerField(default=0,blank=True)
def __str__(self):
    return self.titre

def save(self, *args ,**kwargs):
    #cette partie permet de generer un identifiant unique
    f=self.image.read(1000)
    self.image.seek(0)
    mime=magic.from_buffer(f,mime=True)
    if "video" in mime :
        self.T="short"
    super(post,self).save(*args,**kwargs)

thanks for your helps and sorry for my bad english

CodePudding user response:

Cloudinary does not allow you to upload all types of file , so if you want to upload any kind of file you need to use another object storage or use local media storage.

CodePudding user response:

There are different types of files that are being supported when uploading your assets to your Cloudinary Media Library Account, and it can be identified using the [parameter resource_type][1]. There is also the class CloudinaryField where you can provide the parameter resource_type="auto" to support different file types automatically, for example:

from cloudinary.models import CloudinaryField
from django.contrib import admin
from django.db import models

class Photo(models.Model):
    name = models.CharField(max_length=159)
    image = CloudinaryField(
        "Image",
        resource_type="auto",  # <= Options: image, video, raw, auto
    )

There is a sample project as part of the Cloudinary Python SDK which can be found [here][2].

[1]: https://cloudinary.com/documentation/image_upload_api_reference#:~:text=parameter to attachment).-,resource_type,-String [2]: https://github.com/cloudinary/cloudinary-django-sample

  • Related