Reputation: 768
I am trying to create an app that sends a video file to a encoding service an from the encoding service to an AWS bucket. This would required me to make the file available to a function that handles the upload and update the url of the fil location before saving the object from the admin.
The upload to encoder process returns information in JSON format that show errors or success. I have had no trouble with trying it out on the client side of the site. However I am not quite sure how to go about it using Django's admin.
I have looked at the docs and found the ModelAdmin.add_view(). I am not sure how I can get the name and the path of the uploaded file since this is required for the function uploads to the encoder.
I realize that it is probable best to save all the other information once the process is complete since the encoder sends a ping back.
How do I access the uploaded file to so I can run the encode function on it and is the a way to save all the other information when the service receives a ping back from the service?
Edit:
As requested a timeline/flow of events.
This is how it works right now in the front end:
What I am trying to make sure that the uploading to the encoder is done outside of models.py since it returns some import information in the JSON response which can be used to throwback an error.
Upvotes: 1
Views: 2717
Reputation: 33420
It would probably be better to create a custom storage.
If you only want to override the save function of the admin, then you should override save_model():
def save_model(self, request, obj, form, change):
"""
Given a model instance save it to the database.
"""
# your custom stuff here, this is the file path, change "upload_field_name"
# by your actual FileField name
obj.upload_field_name.path
obj.save()
This code is taken from django/contrib/admin/options.py
, which presents many methods you can override.
Or, you could connect a function to the pre_save signal as such:
from django.db.models import signals
def encode_upload(sender, instance=None, created=None, **kwargs):
# your custom stuff here, this is the file path, change "upload_field_name"
# by your actual FileField name
instance.upload_field_name.path
# change YourModelClass by the name of your actual model class
signals.pre_save.connect(encode_upload, sender=YourModelClass)
This will make encode_upload
to be called before an instance of YourModelClass is saved. Be it in the admin, in other views, anywhere save() is called.
Note that the slot (function/callback connect to the signal) should be connected when the site is started. Use it in models.py
for example.
Learn more about signals.
Upvotes: 3