Get absolute file path of FileField of a model instance in Django - django

I have a page where people can upload files to my server.
I want to do something with the file in Celery. So, I need to know the absolute filepath of the uploaded FileFiled of my Model.
Let's say I queried the model and got the instance.
Now I need to get the absolute file path including the filepath.
obj = Audio.objects.get(pk=1)
I'm currently trying obj.filename and it's only printing the file name and not the absolute path.
I know I can get the upload path I input into upload_to and media directory, but I was wondering if there was a more DRY and automatic approach.
How do I get the absolute path of file which is a file filed in obj?

Found an answer.
I gotta do a .path on the FileField
If I do
obj.audio_file.path
obj is the model instance I queried and audio_file is the filefield

Related

Use already uploaded image in django db

I want to avoid file duplication for if user already have uploaded an image file in db.
Django ImageField adds an suffix to the image file if the file already exists.
I tried using the solution given by allcaps in following question:
Use already uploaded image in Django's ImageField
But it only gives me an drop-down option in the upload section, but it doesn't contain anything.
Sorry for bothering I forgot to register the model I am using to store images. The answer provided in the link work for me after registering remaining model

Django save large files

I'm creating a website where the user is able to upload large files. Around 100-300Mb. I'm using django but I want to know what is the best option to save the files. Should I just add them to my user database? Or should I create a media folder and save the files there. If so then this would mean that I have to save the name of the file in my user database?
Please let me know how you would tackle this issue.
django does not store files in the database, but in media folder. The FileField contains the path to the file(in the media folder). You can change that behavior but it is not recommended(as general practice). What you should consider however is that files of that size will require some more work both in django and the front-end since the upload process will freeze the server. A potential solution to the problem is this: https://pypi.org/project/django-chunked-upload/

Django FileField() disable upload

I have a FileField() in a Django model. On save(), I intercept this and upload the actual file data to a remote-backend/Bucket, then save the URL returned from the service in another field.
The file actually uploads to my file-system (I believe).
How can I disable the FileField() to not actually upload the file to wherever media is stored with Django?
You could use upload_to atttribute of FileField.
Reference:
https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.FileField.upload_to

Pillow upload path in pythonanewhere?

I'm using pythonanywhere.com and would like to attach images for my posts. When installed my model, I have a folder path for downloads like this
image = models.ImageField(upload_to="/static/uploads/",
but when I attach the photo I receive a message
The joined path (/static/uploads/1.jpg) is located outside of the base path component (/home/farmville)
if I specify the full path
/home/username/project/static/uploads/
it's working, but in the template file this path does not work, the image is not :(
How can it can be solved?
The two paths are different things, so using the same path doesn't make sense. The upload_to path is a path to a directory on the server where the image should be uploaded. The path in the template is probably a URL path (it's hard to tell without the actual template) and so it needs to point to where you're serving the uploads from.

Django FileField save the url instead of the path in the database

I have a FileField on my form.
I like the way it behaves, writes the file to MEDIA_ROOT, etc.
I'd like to change what it writes to the database.
If I look in the database I see the path.. /home/user/media/path/to/file.txt what I would like it to write is the URL path without the domain /media/file.txt
Is there an argument to pass into the ModelForm, or Model?
If not, which class do I override?
Seems like bad practice to write absolute path's to a database. I need to be able to dynamically change MEDIA_ROOT & MEDIA_URL. And possibly share this database with other non django applications that would only need the URL of the media.
I don't see a way to save the URL in the database, but you can save a relative path in the database. I suppose you have set upload_to to an absolute path. You can change that to a path relative to your MEDIA_ROOT, for example `upload_to="path/to/". See also the official Documentation on that matter.