Attach files to Django models
Project description
Django Anchor
Django Anchor is a reusable Django app that allows you to attach files to models.
Anchor works very similarly to Django's FileField and ImageField model
fields, but adds a few extra features:
- Images can be resized, converted to another format and otherwise transformed.
- Files are served through signed URLs that can expire after a configurable amount of time, even when using the default file-system storage backend.
Django Anchor is essentially a port of the excellent Active Storage Ruby on Rails feature, but leveraging existing Django abstractions and packages of the Python ecosystem. Some features are not yet implemented, but the core concepts are there two eventually be able to support them.
Installation
Check out the installation guide in the documentation for more details.
Django-anchor is compatible with Django >= 4.2 and Python >= 3.12.
-
Add the
django-anchorpackage to your dependencies. You can do this by running:pip install django-anchoror by adding
django-anchorto yourrequirements.txtorpyproject.tomlfiles if you have one. -
Add
anchortosettings.INSTALLED_APPS -
Add URL configuration to your project:
urlpatterns = [ path('anchor/', include('anchor.urls')), ]
-
Run migrations:
python manage.py migrate
In addition, if you wish to create image variants, a Pillow >= 9.5 should be available in your system.
Usage
💡 Check out the dummy Django project in the test folder for inspiration and the Getting Started guide in the documentation.
Adding files to models
The easiest way to add a file to a model is to add a BlobField to it:
from django.db import models
from anchor.models.fields import SingleAttachmentField
class Movie(models.Model):
title = models.CharField(max_length=100)
cover = SingleAttachmentField()
That's it! No need to run makemigrations or migrate since Django Anchor
doesn't actually need any columns added to the model.
The cover field works just like any other model field:
# Create a new movie
movie = Movie.objects.create(title="My Movie")
# Attach an uploaded file
movie.cover = uploaded_file
# Get a URL to the file
movie.cover.url()
# Get a URL to a miniature version of the file
movie.cover.representation(resize_to_fit=(200, 200), format="webp").url()
# Delete the cover and remove the file from storage
movie.cover.purge()
Using files in templates
Django anchor comes with a handy template tag to render URLs of files you've stored:
{% load anchor %}
<img src="{% representation_url movie.cover resize_to_fit='300x600' format='jpeg' %}">
The above call to representation_url will generate an optimized version of the
movie's cover in JPEG format which fits inside a 300x600 rectangle.
Contributing
PRs and issues are very welcome!
Check out CONTRIBUTING.md to learn how to set up the project locally.
License
This project is released under the MIT License. Check out LICENSE to get the full text of the license.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_anchor-0.8.1.tar.gz.
File metadata
- Download URL: django_anchor-0.8.1.tar.gz
- Upload date:
- Size: 27.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92928d27ebaacbe4694d1190004e8710c159c074377942e6622e7255856ba2c1
|
|
| MD5 |
0378b63ef4009e1b3a6adf2287a667b7
|
|
| BLAKE2b-256 |
1d4e5e186af1dc4d6682843d9ddca86159498b08f5954544ef77acb5cfa924d6
|
Provenance
The following attestation bundles were made for django_anchor-0.8.1.tar.gz:
Publisher:
publish_to_pypi.yml on knifecake/django-anchor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_anchor-0.8.1.tar.gz -
Subject digest:
92928d27ebaacbe4694d1190004e8710c159c074377942e6622e7255856ba2c1 - Sigstore transparency entry: 1076598906
- Sigstore integration time:
-
Permalink:
knifecake/django-anchor@00a019bc7ca249bef8741967f6f93d891bfb48ac -
Branch / Tag:
refs/tags/v0.8.1 - Owner: https://github.com/knifecake
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@00a019bc7ca249bef8741967f6f93d891bfb48ac -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_anchor-0.8.1-py3-none-any.whl.
File metadata
- Download URL: django_anchor-0.8.1-py3-none-any.whl
- Upload date:
- Size: 36.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e386a2f12027a80b023429a246df84ff95db3407da6f51451debea194a1f9d62
|
|
| MD5 |
7aba0a7063625b9f5f317ebefb613546
|
|
| BLAKE2b-256 |
8ea66f4891d666408ae3147b700c5fde067c796a91847ce9d5fa7998fe61106a
|
Provenance
The following attestation bundles were made for django_anchor-0.8.1-py3-none-any.whl:
Publisher:
publish_to_pypi.yml on knifecake/django-anchor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_anchor-0.8.1-py3-none-any.whl -
Subject digest:
e386a2f12027a80b023429a246df84ff95db3407da6f51451debea194a1f9d62 - Sigstore transparency entry: 1076598913
- Sigstore integration time:
-
Permalink:
knifecake/django-anchor@00a019bc7ca249bef8741967f6f93d891bfb48ac -
Branch / Tag:
refs/tags/v0.8.1 - Owner: https://github.com/knifecake
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@00a019bc7ca249bef8741967f6f93d891bfb48ac -
Trigger Event:
release
-
Statement type: