Django Model Fields that are smart.
Project description
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 descibe 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=[
attname='avatar_jpeg', FileDependency(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.
Dependencies
Django (for now only version >= 1.7, will add support for earlier versions later).
Python Pillow - (optional) used for image conversion/resizing.
ffmpeg - (optional) for video conversion. (could be easily adopted for libav).
BeautifulSoup4 - (optional) for HTML stripping
django-crispy-forms - (optional) for ajax uploading.
Plupload - (optional) for ajax uploading.
Changelog
1.0.2
Introduced pre_processor.
Made UploadTo serializible.
Got rid of custom handlers.
Minor bugfixes.
1.0.0
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-smartfields-1.0.2.tar.gz
.
File metadata
- Download URL: django-smartfields-1.0.2.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80e0ca31cfa233c20deb77ea8f1358d61d57f878e7a3c4c01092c57d1394287a |
|
MD5 | a3d8d83c35160b635ee27196ab4deae3 |
|
BLAKE2b-256 | 392d2403e2433cb349192c9a41829682543d4017c215fb3d662ad5fc56bd4936 |
File details
Details for the file django_smartfields-1.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_smartfields-1.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 43.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fdc3663d4249322aeeec9a11f9bfe50e80c7083e1ff314cf054f816d7944cc1 |
|
MD5 | 3a7c6eec890406d294de4767167bce77 |
|
BLAKE2b-256 | d81dda19480a250517e92f1bc864c2f469af2b16f447a09f2237929793841fd5 |