diff --git a/README.md b/README.md index 751e5123..9fc12f44 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,21 @@ SETUP USAGE ----- +## Django models +In `models.py`, + +```python + from django.db import models + + from django_summernote.fields import SummernoteTextField + + class Post(models.Model): + content = SummernoteTextField() +``` + +`SummernoteTextField` inherits all of the properties of a regular `TextField`. Using this method, the `SummernoteWidget` will automatically be used in the django admin. + + ## Django admin site ### Apply summernote to all TextField in model In `admin.py`, diff --git a/django_summernote/apps.py b/django_summernote/apps.py index 11f59736..631f7380 100644 --- a/django_summernote/apps.py +++ b/django_summernote/apps.py @@ -1,7 +1,7 @@ from django.apps import AppConfig from django.conf import settings as django_settings from django_summernote.utils import ( - LANG_TO_LOCALE, uploaded_filepath, get_theme_files + LANG_TO_LOCALE, get_theme_files ) @@ -29,7 +29,7 @@ def get_default_config(self): # Attachment settings 'disable_attachment': False, - 'attachment_upload_to': uploaded_filepath, + 'attachment_upload_to': 'django_summernote.utils.uploaded_filepath', 'attachment_storage_class': None, 'attachment_filesize_limit': 1024 * 1024, 'attachment_require_authentication': False, diff --git a/django_summernote/utils.py b/django_summernote/utils.py index 2e987626..1194d2a9 100644 --- a/django_summernote/utils.py +++ b/django_summernote/utils.py @@ -114,6 +114,25 @@ } +def get_by_qname(path, desc): + try: + dot = path.rindex('.') + except ValueError: + raise ImproperlyConfigured("%s isn't a %s module." % (path, desc)) + module, objname = path[:dot], path[dot + 1:] + try: + mod = import_module(module) + except ImportError as e: + raise ImproperlyConfigured('Error importing %s module %s: "%s"' % + (desc, module, e)) + try: + obj = getattr(mod, objname) + return obj + except AttributeError: + raise ImproperlyConfigured('%s module "%s" does not define "%s"' + % (desc[0].upper() + desc[1:], module, objname)) + + def using_config(_func=None): """ This allows a function to use Summernote configuration @@ -204,7 +223,7 @@ def get_attachment_upload_to(): """ Return 'attachment_upload_to' from configuration """ - return config['attachment_upload_to'] + return get_by_qname(config['attachment_upload_to'], 'attachment_upload_to') @using_config