bugfix
[redakcja.git] / src / catalogue / admin.py
index 95da499..91656f1 100644 (file)
@@ -4,60 +4,95 @@ from .wikidata import WikidataAdminMixin
 
 
 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
 
 
 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
-    list_display = ["first_name", "last_name", 'status', "year_of_death", "priority", "wikidata_link"]
-    list_filter = ['year_of_death', 'priority', 'collections', 'status']
+    list_display = [
+        "first_name",
+        "last_name",
+        "status",
+        "year_of_death",
+        "priority",
+        "wikidata_link",
+        "slug",
+    ]
+    list_filter = ["year_of_death", "priority", "collections", "status"]
     search_fields = ["first_name", "last_name", "wikidata"]
     prepopulated_fields = {"slug": ("first_name", "last_name")}
     search_fields = ["first_name", "last_name", "wikidata"]
     prepopulated_fields = {"slug": ("first_name", "last_name")}
-    autocomplete_fields = ['collections']
+    autocomplete_fields = ["collections"]
 
 
 admin.site.register(models.Author, AuthorAdmin)
 
 
 class BookAdmin(WikidataAdminMixin, admin.ModelAdmin):
 
 
 admin.site.register(models.Author, AuthorAdmin)
 
 
 class BookAdmin(WikidataAdminMixin, admin.ModelAdmin):
-    list_display = ["title", 'authors_str', 'translators_str', 'language', 'pd_year', 'priority', 'wikidata_link']
-    search_fields = ["title", 'wikidata']
-    autocomplete_fields = ["authors", "translators", "based_on", 'collections']
+    list_display = [
+        "title",
+        "authors_str",
+        "translators_str",
+        "language",
+        "pd_year",
+        "priority",
+        "wikidata_link",
+    ]
+    search_fields = ["title", "wikidata"]
+    autocomplete_fields = ["authors", "translators", "based_on", "collections"]
     prepopulated_fields = {"slug": ("title",)}
     prepopulated_fields = {"slug": ("title",)}
-    list_filter = ['language', 'pd_year', 'collections']
-    readonly_fields = ['wikidata_link']
+    list_filter = ["language", "pd_year", "collections"]
+    readonly_fields = ["wikidata_link"]
     fieldsets = [
     fieldsets = [
-        (None, {'fields': [
-            ('wikidata', 'wikidata_link'),
-        ]}),
-        ('Identification', {'fields': [
-            'title', 'slug', 'authors', 'translators', 'language', 
-            'based_on',
-            'pd_year',
-        ]}),
-        ('Plan', {'fields': [
-            'scans_source',
-            'text_source',
-            'priority',
-            'collections',
-            'notes',
-        ]}),
+        (None, {"fields": [("wikidata", "wikidata_link")]}),
+        (
+            "Identification",
+            {
+                "fields": [
+                    "title",
+                    "slug",
+                    "authors",
+                    "translators",
+                    "language",
+                    "based_on",
+                    "pd_year",
+                ]
+            },
+        ),
+        (
+            "Plan",
+            {
+                "fields": [
+                    "scans_source",
+                    "text_source",
+                    "priority",
+                    "collections",
+                    "notes",
+                ]
+            },
+        ),
     ]
 
     ]
 
+    def get_queryset(self, request):
+        qs = super().get_queryset(request)
+        if request.resolver_match.view_name.endswith("changelist"):
+            qs = qs.prefetch_related("authors", "translators")
+        return qs
+
 
 admin.site.register(models.Book, BookAdmin)
 
 
 class AuthorInline(admin.TabularInline):
     model = models.Author.collections.through
 
 admin.site.register(models.Book, BookAdmin)
 
 
 class AuthorInline(admin.TabularInline):
     model = models.Author.collections.through
-    autocomplete_fields = ['author']
+    autocomplete_fields = ["author"]
 
 
 class BookInline(admin.TabularInline):
     model = models.Book.collections.through
 
 
 class BookInline(admin.TabularInline):
     model = models.Book.collections.through
-    autocomplete_fields = ['book']
+    autocomplete_fields = ["book"]
 
 
 class CollectionAdmin(admin.ModelAdmin):
 
 
 class CollectionAdmin(admin.ModelAdmin):
-    list_display = ['name']
+    list_display = ["name"]
     autocomplete_fields = []
     autocomplete_fields = []
-    prepopulated_fields = {'slug': ('name',)}
-    search_fields = ['name']
+    prepopulated_fields = {"slug": ("name",)}
+    search_fields = ["name"]
     inlines = [AuthorInline, BookInline]
 
     inlines = [AuthorInline, BookInline]
 
+
 admin.site.register(models.Collection, CollectionAdmin)
 admin.site.register(models.Collection, CollectionAdmin)