Django with Pluggable MongoDB Storage troubles -


i'm trying use django, , mongoengine provide storage backend gridfs. still have mysql database.

i'm running strange (to me) error when i'm deleting django admin , wondering if doing incorrectly.

my code looks this:

# settings.py mongoengine import connect connect("mongo_storage")  # models.py mongoengine.django.storage import gridfsstorage class myfile(models.model):     name = models.charfield(max_length=50)     content = models.filefield(upload_to="appsfiles", storage=gridfsstorage())     creation_time = models.datetimefield(auto_now_add=true)     last_update_time = models.datetimefield(auto_now=true) 

i able upload files fine, when delete them, seems break , mongo database seems in unworkable state until manually delete filedocument.objects. when happens can't upload files or delete them django interface.

from stack trace have:

/home/projects/vector/src/mongoengine/django/storage.py in _get_doc_with_name         doc = [d d in docs if getattr(d, self.field).name == name] ... ▼ local vars variable    value _[1]     [] d     docs     error in formatting: cannot set options after executing query name     u'testfile.pdf' self      /home/projects/vector/src/mongoengine/fields.py in __getattr__         raise attributeerror  

am using feature incorrectly?

update:

thanks @zeekay's answer able working gridfs storage plugin work. ended not using mongoengine @ all. put adapted solution on github. there clear sample project showing how use it. uploaded project pypi.

another update:

i'd highly recommend django-storages project. has lots of storage backed options , used many more people original proposed solution.

i think better off not using mongoengine this, haven't had luck either. here drop-in replacement mongoengine.django.storage.gridfsstorage, works admin.

from django.core.files.storage import storage django.conf import settings  pymongo import connection gridfs import gridfs  class gridfsstorage(storage):     def __init__(self, host='localhost', port=27017, collection='fs'):         s in ('host', 'port', 'collection'):             name = 'gridfs_' + s.upper()             if hasattr(settings, name):                 setattr(self, s, getattr(settings, name))         s, v in zip(('host', 'port', 'collection'), (host, port, collection)):             if v:                 setattr(self, s, v)         self.db = connection(host=self.host, port=self.port)[self.collection]         self.fs = gridfs(self.db)      def _save(self, name, content):         self.fs.put(content, filename=name)         return name      def _open(self, name, *args, **kwars):         return self.fs.get_last_version(filename=name)      def delete(self, name):         oid = fs.get_last_version(filename=name)._id         self.fs.delete(oid)      def exists(self, name):         return self.fs.exists({'filename': name})      def size(self, name):         return self.fs.get_last_version(filename=name).length 

gridfs_host, gridfs_port , gridfs_collection can defined in settings or passed host, port, collection keyword arguments gridfsstorage in model's filefield.

i referred django's custom storage documenation, , loosely followed this answer similar question.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -