Hello good morning gentle men, I am working on a project and I have a model that stores photos and automatically upload them on azure blobs storage, this model automatically rename photos according to user input, my problem is I need to collect 200 photos all different names, how best can I do this? I already have 20 fields for testing but I was thinking what if a user was uploading and get an error? I dont want them to start all over again assuming I put the entire 200 photo fields
#File/photo upload
22 messages · Page 1 of 1 (latest)
You make one model instance per photo
could you clarify this
this is what my current model looks like
from django.db import models
import os
class Project_name(models.Model):
project_name = models.CharField(max_length=255, null=True, blank=True)
class Project(models.Model):
project_name = models.ForeignKey(Project_name, on_delete=models.CASCADE, null=True, blank=True)
Fila_XX_F = models.CharField(max_length=255, null=True, blank=True)
Fila_XX_E = models.CharField(max_length=255, null=True, blank=True)
AF_NN_XX = models.CharField(max_length=255, null=True, blank=True)
Fila_XX_F_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
Fila_XX_E_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
AF_NN_XX_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
def save(self, *args, **kwargs):
if self.Fila_XX_F_photo and self.Fila_XX_F:
ext = os.path.splitext(self.Fila_XX_F_photo.name)[1]
self.Fila_XX_F_photo.name = f"Fila_{self.Fila_XX_F}_F{ext}"
self.Fila_XX_E_photo.name = f"Fila_{self.Fila_XX_E}_E{ext}"
self.AF_NN_XX_photo.name = f"Fila_AF_NN_{self.AF_NN_XX}{ext}"
super().save(*args, **kwargs)
use code formatting as shown in #readme-1st please
I could give a generic example, but your model seem to be more specific, if you elaborate context of it's usage I also might be able to give better suggestion
from django.db import models
import os
class Project_name(models.Model):
project_name = models.CharField(max_length=255, null=True, blank=True)
class Project(models.Model):
project_name = models.ForeignKey(Project_name, on_delete=models.CASCADE, null=True, blank=True)
Fila_XX_F = models.CharField(max_length=255, null=True, blank=True)
Fila_XX_E = models.CharField(max_length=255, null=True, blank=True)
AF_NN_XX = models.CharField(max_length=255, null=True, blank=True)
Fila_XX_F_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
Fila_XX_E_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
AF_NN_XX_photo = models.FileField(upload_to='project_photos/', blank=True, null=True)
def save(self, args, **kwargs):
if self.Fila_XX_F_photo and self.Fila_XX_F:
ext = os.path.splitext(self.Fila_XX_F_photo.name)[1]
self.Fila_XX_Fphoto.name = f"Fila{self.Fila_XX_F}_F{ext}"
self.Fila_XX_Ephoto.name = f"Fila{self.Fila_XX_E}_E{ext}"
self.AF_NN_XX_photo.name = f"Fila_AFNN{self.AF_NN_XX}{ext}"
super().save(args, **kwargs)
The context of its usage is that, after a project has been completed, there is a database that stores photos of every part of the items used in the project. This way, we have a folder for each project containing all its photos, already renamed according to their part names.
The photos are too many, and I am trying to simplify the process for users. They can easily click 'Upload,' select a photo, enter the part number, and the photo will automatically be renamed with its initials.
May I ask why Project_name is a separate model? Even if it has more fields you didn't include or planning to add this looks off
My initial plan was to have separate models and reference project_name as a ForeignKey
from pathlib import Path
class Project(models.Model):
name = models.CharField()
class ProjectItem(models.Model):
def make_photo_path(obj, filename):
path = Path('project_photos') / "_".join((self.code, self.subcode))
path = path.with_suffix(Path(filename).suffix)
return str(path) # check if django works with Path object now, remove str then
project = models.ForeignKey("Project")
code = models.CharField()
subcode = models.CharField()
photo = models.ImageField(upload_to=make_photo_path)
That what I feel it should be like
For what benefit? Right now it looks just like reducing performance (a bit) for no reason
this way, I can make sections in the view that allow users to save progress, now when you point it out I figured its on no use actually, I think I'm gonna use pagination and save draft option
You mean like save fields without filling project name or vice versa?
You can store draft data in session too for example
yes, without filling the project name
For normal (non-relational and non-file) fields I'd use session
For file fields if you go with ProjectItem approach you could make project nullable and save them without reference
why is it without reference?
To allow them being saved before Project itself is saved (if user haven't filled all fields yet)
Hello @cedar swan , could you please use a more inclusive terms to call people next time? This break the Discord [first rule](#rules message). Despite the fact that "gentle men" is really polite, I'm not a man and feel less concerned from your post.