I am trying to production-deploy a Django project for the first time. My pre-Django implementation contains quite a bit of data (thousands of images, thousands of entries in the database). I have written a custom manage.py populate_db
command that parses the dump of the old database and creates the new Django database based on my models. However I am stuck with adding all the files.
My initial deployment plan is to create the database locally once, sFTP-upload all images to the media folder on the server, do the same with the database and that's it. For this, I need the database to reference the image files in the media folder. As far as I understand, Django does it simply by filename. So far, I've been able to do this:
def _add_image_files(self, user: str):
i = 0
for f in local_files:
i += 1
photo = Photo(album=1, number=i, back=False, added_by=user)
from django.core.files import File
photo.image.save(f.name, File(open(f.path, 'rb')))
where Photo is defined as
class Photo(models.Model):
album = models.IntegerField(null=True)
number = models.IntegerField(null=True)
image = models.ImageField(null=True, upload_to='photo_image_files')
However Django obviously duplicates every file by reading and writing it to the media folder. I don't need this duplication, I only want to add a reference to each file to the database.
How can I achieve it? Alternatively, if this is for some reason a poor approach, please let me know a better way. Thanks.