Skip to content

Commit

Permalink
override cache name
Browse files Browse the repository at this point in the history
  • Loading branch information
knifecake committed Dec 22, 2024
1 parent a8bafe5 commit b8b76c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions anchor/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
9 changes: 9 additions & 0 deletions anchor/models/fields/single_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -105,6 +112,8 @@ def instance_attr(i):


class SingleAttachmentField(GenericRelation):
rel_class = SingleAttachmentRel

def __init__(
self,
upload_to: str | Callable[[Blob], str] = None,
Expand Down
1 change: 1 addition & 0 deletions demo/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"querystring_auth": True,
"querystring_expire": 600,
"signature_version": "s3v4",
"file_overwrite": False,
},
}

Expand Down
6 changes: 6 additions & 0 deletions demo/movies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit b8b76c6

Please sign in to comment.