django


Import files into a colection from CLI (django command)


I'm trying to build a django command to upload files and create associated pages for them.
My docs are PDF files, and my problem is to automatically "upload" those files into the right target "media" directory, without explicit copying them with my command script from the 'docs repository' to the MEDIA_ROOT defined directory.
I've tryed to use:
Code
f = File(open(file_path, 'r'))
# models.OfficeDocument is an inheritor of BaseDocument class
new_document, created = models.OfficeDocument.objects.get_or_create(title=title,
collection=collection,
file=f)
Error
SuspiciousFileOperation: The joined path (<my_local_path>) is located outside of the base path component (<MEDIA_ROOT path>)
but wagtail says me I'm not in the right directory (not in MEDIA_ROOT)
How can I do that ?
Thanks !
You are trying to save the documents with their files in the current location, without copying them to the new location first. You could add some code to copy the files to the right location, or it seems reasonable to mimic what the Wagtail documents add view does here—instantiate a modelform, clean it, and call its save method. That will handle the saving of the document as it has the upload_to property configured on the file field.
Try:
from wagtail.wagtailcore.models import get_root_collection_id
collection = get_root_collecion_id()
user = some_user_you_will_attribute_these_to
doc = Document(uploaded_by_user=user)
upload_dict = {
'title': some_title,
'file': f,
'collection': collection,
'tags': '',
}
form = DocumentForm(upload_dict, f, instance=doc, user=request.user)
if form.is_valid():
form.save()
You might find that the file object needs to be something like a wrapped Django UploadedFile instance instead, see https://docs.djangoproject.com/en/1.11/ref/files/uploads/

Related Links

django login with google app acoounts
Show 0.00 as comparison of NONE in django?
Get url configuration in django 404 pages when Mezzanine is installed
Django app works via manage.py runserver but page rendered via mod_uwsgi fails to refer to static elements
Django counting double
Django 1.5: UserCreationForm & Custom Auth Model
django-mailer creates multiple db entries from email address
Understanding an existing big web application written in Django
404 not found on tastypie resource's POST method
Django pagination to paginate table data
ModelForms and ForeignKeys
Django filter the queryset of ModelChoiceField
Django always returns error 404 with nginx
django sending mail using pre_save
Django TastyPie NotFound(“Invalid resource lookup data provided (mismatched type).”)
How to have django compile .mo files at startup?

Categories

HOME
time-complexity
redux-form
jax-rs
angular2-material
jelastic
devise
shiro
opendj
tivoli
scripting
dojo
maxscript
tvos
nuget-package
boto3
recordrtc
stellar.js
symbol
altera
event-sourcing
breadth-first-search
parcelable
object-storage
jquery-callback
genexus-sd
reverse
sql-delete
xajax
protege4
flatmap
c9.io
comparison-operators
optimizely
facebook-pixel
podscms
zenhub
tinkerpop
arules
swingworker
rxtx
restore
docpad
reporting-services-2012
redbeard-ios
gcloud-ruby
ascii-art
dynamics-nav-2016
themoviedb-api
forwarding
mod-proxy
uncompress
avahi
mass-assignment
execve
primitive-types
webfinger
human-computer-interface
rmysql
with-statement
codeplex
rcaller
cancellation
grails-plugin-rabbitmq
gnumeric
dot42
rfc5322
ebay-lms
dolby-audio-api
composite-component
onload-event
mantle
panda3d
patricia-trie
jsr286
stress
mysql-error-1205
entitydatasource
umra
w3c-geolocation
instantiationexception
vs-android
sifr
perl5.12
localizable.strings
soappy

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile