I need to save files, but the number of files is uncertain, and in one request I can add a file, then in another request post, I can add one more file ...
For this reason I thought that creating a JSONField type field could help me in this matter, but I don't know if it is possible and if it is possible I have no idea how to implement it in Django.
Here is an example of how I currently upload a single file to Django Model:
import uuid
import time
from users.models import User
from django.db import models
def upload_location(instance, filename):
filebase, extension = filename.split('.')
milliseconds = int(round(time.time() * 1000))
return 'events/thumbnail_images/%s__%s__%s.%s' % (instance.user_id, instance.name, milliseconds, extension)
class Test(models.Model):
id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False, null=False)
image_link = models.FileField(upload_to=upload_location)
user = models.ForeignKey(
User,
null=True,
blank=False,
on_delete=models.CASCADE
)
As I'm using Postgres relational database, and I'm not sure how many files the user will want to save, I figured using JSONField, so that as the user adds files, the 'path+filename' is also added.
It's possible? How to make?