diff --git a/anchor/admin.py b/anchor/admin.py index 5975703..dcec232 100644 --- a/anchor/admin.py +++ b/anchor/admin.py @@ -42,6 +42,8 @@ def save_m2m(self): class AttachmentAdmin(admin.ModelAdmin): list_display = ("blob", "name", "order", "content_type", "object_id") raw_id_fields = ("blob",) + list_filter = ("content_type",) + search_fields = ("id", "object_id", "blob__id") def get_queryset(self, request): return ( diff --git a/anchor/models/fields/single_attachment.py b/anchor/models/fields/single_attachment.py index 3b8a06e..6351695 100644 --- a/anchor/models/fields/single_attachment.py +++ b/anchor/models/fields/single_attachment.py @@ -5,11 +5,18 @@ from django.db import models from django.db.models import Model from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor +from django.utils.functional import cached_property from django.utils.text import capfirst from anchor.models import Attachment, Blob +class SingleAttachmentRel(GenericRel): + @cached_property + def cache_name(self): + return self.field.attname + + class ReverseSingleAttachmentDescriptor(ReverseOneToOneDescriptor): def __init__( self, @@ -105,6 +112,8 @@ def instance_attr(i): class SingleAttachmentField(GenericRelation): + rel_class = SingleAttachmentRel + def __init__( self, upload_to: str | Callable[[Blob], str] = None, diff --git a/demo/demo/settings.py b/demo/demo/settings.py index 91d1cc3..e635d66 100644 --- a/demo/demo/settings.py +++ b/demo/demo/settings.py @@ -130,6 +130,7 @@ "querystring_auth": True, "querystring_expire": 600, "signature_version": "s3v4", + "file_overwrite": False, }, } diff --git a/demo/movies/models.py b/demo/movies/models.py index 6d16419..a96a3b1 100644 --- a/demo/movies/models.py +++ b/demo/movies/models.py @@ -11,6 +11,12 @@ class Movie(models.Model): upload_to="movie-covers", blank=True, help_text="A colorful image of the movie." ) + credits = SingleAttachmentField( + upload_to="movie-credits", + blank=True, + help_text="A screenshot of the movie credits screen.", + ) + def get_absolute_url(self): return reverse("movies:movie_detail", kwargs={"pk": self.pk})