1
0
Fork 0
mirror of https://github.com/Findus23/PaperLibrary.git synced 2024-09-08 03:13:48 +02:00

improve queries

This commit is contained in:
Lukas Winkler 2024-04-17 16:55:14 +02:00
parent c2cd2ea71e
commit f1ac6d8376
Signed by: lukas
GPG key ID: 54DE4D798D244853
2 changed files with 22 additions and 5 deletions

View file

@ -47,6 +47,8 @@ class PaperSerializer(serializers.HyperlinkedModelSerializer):
recommended_by = serializers.SlugRelatedField("name", many=True, queryset=Author.objects.all())
pdfs = PDFSerializer(many=True)
id = serializers.ReadOnlyField()
ads_version = serializers.ReadOnlyField()
citename = serializers.ReadOnlyField()
notes_md = serializers.CharField(source='note.text_md')
notes_html = serializers.CharField(source='note.text_html')
notes_updated_at = serializers.DateTimeField(source='note.updated_at')

View file

@ -1,4 +1,5 @@
# Create your views here.
from django.db.models import Prefetch
from django.http import HttpResponse, HttpRequest
from rest_framework import viewsets, permissions
from rest_framework.decorators import api_view
@ -9,16 +10,30 @@ from library.utils.bibtex import papers_to_bibtex_file
class PaperViewSet(viewsets.ModelViewSet):
queryset = Paper.objects.all().select_related("first_author") \
.select_related("publication").select_related("doctype").select_related("note") \
.prefetch_related("pdfs").prefetch_related("authors").prefetch_related("tags") \
.prefetch_related("keywords").prefetch_related("recommended_by")
queryset = (Paper.objects.all()
.select_related("first_author")
.select_related("publication")
.select_related("doctype")
.select_related("note")
.prefetch_related("pdfs")
.prefetch_related("authors")
.prefetch_related("tags")
.prefetch_related("keywords")
.prefetch_related("recommended_by")
)
serializer_class = PaperSerializer
permission_classes = [permissions.IsAuthenticated]
class AuthorViewSet(viewsets.ModelViewSet):
queryset = Author.objects.all().prefetch_related("papers").prefetch_related("papers__pdfs")
queryset = (Author.objects.all()
.prefetch_related(Prefetch(
"papers", queryset=Paper.objects.defer("abstract", "bibtex")
))
.prefetch_related(Prefetch(
"papers__pdfs", queryset=PDF.objects.defer("full_text")
))
)
serializer_class = AuthorSerializer
permission_classes = [permissions.IsAuthenticated]