Skip to main content

Django Model Fields that are smart.

Project description

Documentation Status Latest Version Code Health Tests Coverage Travis-CI

Django Model Fields that are smart.

This app introduces a declarative way of handling fields’ values. It can be especially useful when one field depends on a value from another field, even if a field depends on itself. At first it might sound useless, but, as it turns out, it is an amazing concept that helps in writing clear, concise and DRY code.

Best way to describe is on a simple example. Let’s say there is a field where you store a custom html page and you would like to have another field attached to the same model store the same page but with html tags stripped out, moreover you would like it to update whenever the first field changes it’s value. A common way to handle that issue is to overwrite model’s save method and put all the logic there, right? What if you could just give a field a function that does the stripping and everything else is taking care of? Wouldn’t that be nice, huh? Well, that’s one of many things this app let’s you do.

Another great example is django’s ImageField that can update width_field and height_field whenever image is changed. This app uses similar concepts to achive that functionality. But here is a more powerful example that demonstrates the value of this app. Let’s say you would like to have a user be able to upload an image in any format and automatically add another version of this image converted to JPEG and shrunk to fit in a box size of 1024x768. Here is how it could look with utilization of django-smartfields:

from django.db import models

from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor

class User(models.Model):
    # ... more fields ....
    avatar = fields.ImageField(upload_to='avatar', dependencies=[
        FileDependency(attname='avatar_jpeg', processor=ImageProcessor(
            format='JPEG', scale={'max_width': 1024, 'max_height': 768})),
    ])
    avatar_jpeg = fields.ImageField(upload_to='avatar')
    # ... more fields ...

That’s it. Did I mention that it will also clean up old files, when new ones are uploaded?

So, hopefully I got you convinced to give this app a try. There is full documentation also on the way, but for now you can check out ‘tests’ folder for some examples.

Django settings

Required django apps for most of the functionality:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.contenttypes',
    'django.contrib.sites',

    'smartfields',

    # optional, needed for forms
    'crispy_forms'
]

Other required settings

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware'
]

SITE_ID = 1

Dependencies

  • Django versions >= 1.7 (should aslo work for 2.x)

  • Python Pillow - (optional) used for image conversion/resizing. AND/OR

  • Wand - (optional) also for image processing.

  • ffmpeg - (optional) for video conversion. (can be easily adopted for avconv).

  • BeautifulSoup4 - (optional) for HTML stripping

  • lxml - (optional) for BeautifulSoup.

  • django-crispy-forms - (optional) for ajax uploading.

  • Plupload - (optional) for ajax uploading.

  • Bootstrap3 - (optional) for ajax uploading.

Changelog

1.1.0

  • renamed Dependency.async to Dependency.async_. Fix for #16. Thanks @zglennie

  • Fix compatibility with Django=2.x:

    • Added app_name='smartifelds' to urls.py file

    • Stop using _size and _set_size() attributes in NamedTemporaryFile, since those where only available in Django=1.x

1.0.7

  • added gis fields.

  • made lxml a default parser for HTMLProcessor.

1.0.6

  • added RenameFileProcessor

1.0.5

  • minor bug fixes.

1.0.4

  • Switched to MIT License

  • Added stashed_value to processors.

1.0.3

  • Added support for Wand with WandImageProcessor.

  • Made it compatible with Django 1.8

  • Updated compiled JavaScript file.

1.0.2

  • Introduced pre_processor.

  • Made UploadTo serializible.

  • Got rid of custom handlers.

  • Minor bugfixes.

1.0.0

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-smartfields-1.1.0.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

django_smartfields-1.1.0-py2.py3-none-any.whl (44.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-smartfields-1.1.0.tar.gz.

File metadata

  • Download URL: django-smartfields-1.1.0.tar.gz
  • Upload date:
  • Size: 34.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for django-smartfields-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c58a6025c4cd121bd348d636afdef2ee34daee37c45b60939f3a7339410479a1
MD5 aac5765659e67d244666a4c002929116
BLAKE2b-256 7a8290fc3cdeb08f9253c8075ca638db65ef909f9a8fb34d6c009c95de4353ca

See more details on using hashes here.

File details

Details for the file django_smartfields-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_smartfields-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 44.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for django_smartfields-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8c6a7d150c9c68f59a13bcf0d1c2048b84470c49e87de785da5bb92c4197569a
MD5 604ee99fb471a94789a1b1e3e010f8ec
BLAKE2b-256 89ce234c7619a22af0e9bd3717ce8c89021f1183f4fb49cd98b8291ed27dc7d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page