How to upload images of 10k movies in one go in Django? - python

I am creating the website which contains 10k movies and it has an attribute called movies_logo which stores the logo of the respected movie .So is there any way so that I can upload all the logo of the respected movies in one go because uploading logo of each movie will take lot of time.This is my models.py.
from django.contrib.auth.models import Permission, User
from django.db import models
from decimal import Decimal
class Picture(models.Model):
user = models.ForeignKey(User, default=1)
picture_id = models.IntegerField(default=0)
picture_title = models.CharField(max_length=500)
genres = models.CharField(max_length=1000)
ratings = models.DecimalField(max_digits=10,decimal_places=1)
picture_logo = models.ImageField()
def __str__(self):
return self.picture_title

If you don't the save location for the images, then upload a single picture and see where it gets stored and with what name. If you don't like the naming, you can change it. Once that is figured out, you can simply go to your mysql command-line, and set the image field in the database to the file name and structure you have decided and then upload / move the images to the correct location. If you want, you can use a for loop to set the image field instead of writing SQL.

Related

change django upload folder based on queryset

Gday, I'm working on a section of my data management project where users will be able to upload premade data for it to be parsed and inputted into the database. I am currently stuck on uploading files. The upload field will be on the main page for the specific dataset, which is navigated to by using the dataset id. What I would like is for any files uploaded in on that page to be saved in a directory such as "/projectroot/uploads/dataset_name". is this possible?
I think you are looking for something like this.
def my_memory_file_name(instance, filename):
return '/'.join(['my_memory', instance.user.username, filename])
class MyMemory(models.Model):
title = models.CharField(max_length=150, null=True, blank=True)
situation = models.TextField(help_text="Explain the situation of screenshot in few words")
date = models.DateField(null=True, blank=True, help_text="Date is not a required field")
screenshot = models.FileField(upload_to=my_memory_file_name)

How to design to avoid duplicate column in two models that have the same ForeignKey?

I have the following models where a User has many Photo objects and many Album objects. However, after a User has added a given photo they may choose to put it in a single Album (a Photo does not have to have an album linked to it). They may also choose to take it out of the album and or put it in a different album.
class User(models.Model):
id = ...
class Photo(models.Model):
id = ...
user = models.ForeignKey(User, ...)
album = models.ForeignKey(Album, null=True...)
class Album(models.Model):
id = ...
user = models.ForeignKey(User, ...)
How do I get around the redundant user column (Album.user and Photo.user) when a Photo is associated with an Album to avoid db anomalies?
I can't get rid of the user column on Photo since I wouldn't be able to get it if it is not linked to an album. The same goes for Album. I have some experience with normalizing tables but I don't know how to avoid this without using db constraints.

Wagtail models.py: How to create Custom folder in Media?

In Wagtail, How to add custom folder in Media and sync it into the database (Example) ?
NOTE: The Wagtail's Collection function is good, but for more than 1000 images/documents into a single folder will be pretty awkward to manage for I in the future (Ex: migration,...), so there is nothing to mention about Collection function in this question.
# If in Django (models.py) works:
class post(models.Model):
img = models.ImageField(upload_to='posts')
# So how in Wagtail (models.py) works:
class Posts(models.Model):
img = models.ForeignKey(
"wagtailimages.Image",
👉🏽 upload_to='posts', # How to add this line correctly ?
on_delete=models.SET_NULL,
null=True,
blank=False,
related_name="+",
)
Idea for Media Folder in Wagtail:
Media
authors
images
original_images
posts
images
original_images
...
If you want specific folders for specific models, you can set a specific folder by customizing the upload_to attribute in the model. Otherwise, the only option I know is to create collections.

How to save an image in a directory named on the User?

I am using Django2.2 and I am currently learning Django. I have created a model where I have to post an image of a certain thing and that model is connected with a User. I want to save the image on a directory named on that certain user
I have a custom User Model where I created a field called Profile Photo and that profile photo is saved to a directory named on that User.But then I created another application called 'Product' and there I created many fields including an image field.I am trying to save that image on that directory named on that specific User.
def user_directory_path(instance, filename):
return 'media/%s/%s' % (instance.username, filename)
class Products(models.Model):
title = models.CharField(max_length=100)
body = models.CharField(max_length=1000)
image = models.ImageField(upload_to = user_directory_path)
product_user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
When I try to save a Product and error occurs.
'Products' object has no attribute 'username'
Is there any successful ways to do it.
A Products instance indeed has no username attribute. The product_user has, you thus can change this to:
def user_directory_path(instance, filename):
return 'media/%s/%s' % (instance.product_user.username, filename)
Note: models have usually singular names, so Product, instead of Products.

Save OneToManyRelationship based on url input in Django

I have two database which are OneToManyRelationship as shown below.
I am preparing interface for Data to be uploaded.
I want to specify the project attribute in Data model by inputting the url of the Project path like http://project/13.
Does anyone know how I can construct the relationship from url input of parent data?
Models.py
class Project(models.Model):
project = models.CharField(max_length=50, blank=True)
version = models.IntegerField(default=0)
class Data(models.Model):
project=models.ForeignKey(project)
csv=models.FileField(upload_to=dir_path)
If url in Project model sits in project field then you can do something like
# since project attribute is not unique, several ones can match so we pick first
project_object = Project.objects.filter(project=url).first()
if not project_object:
# error cant find it
Data.objects.create(
project=project_object,
csv=...,
)

Categories