Skip to main content

Attach files to Django models

Project description

Django Anchor

Test Documentation Status

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.11.

  1. Add the django-anchor package to your dependencies. You can do this by running:

    pip install django-anchor
    

    or by adding django-anchor to your requirements.txt or pyproject.toml files if you have one.

  2. Add anchor to settings.INSTALLED_APPS

  3. Add URL configuration to your project:

    urlpatterns = [
        path('anchor/', include('anchor.urls')),
    ]
    
  4. 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


Download files

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

Source Distribution

django_anchor-0.6.2.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_anchor-0.6.2-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file django_anchor-0.6.2.tar.gz.

File metadata

  • Download URL: django_anchor-0.6.2.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_anchor-0.6.2.tar.gz
Algorithm Hash digest
SHA256 cc89ec75c25980e2b80ff345db860e9fbb1ffb11cb4a0e6470483777889fe4d3
MD5 25e7927b2848c6c58aff38ef8fa75238
BLAKE2b-256 e561cad556fd824c6c06a09f11acebcd0011a82e777ef3a513bf9db7d18eb76f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_anchor-0.6.2.tar.gz:

Publisher: publish_to_pypi.yml on knifecake/django-anchor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_anchor-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: django_anchor-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_anchor-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8c5242aea7237f1877f00377db00df536b991ffb609b22a6ee7cd242ef681968
MD5 44c73cb2e88e4c493998fa33a41fbf7c
BLAKE2b-256 53aab72e91b02bd03f4d64ae9df1bb9f78ccaf98c2cee0c60a0d4b01c48a1b00

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_anchor-0.6.2-py3-none-any.whl:

Publisher: publish_to_pypi.yml on knifecake/django-anchor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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