mirror of
https://github.com/Findus23/PaperLibrary.git
synced 2024-09-08 03:13:48 +02:00
improve queries
This commit is contained in:
parent
c2cd2ea71e
commit
f1ac6d8376
2 changed files with 22 additions and 5 deletions
|
@ -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')
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
Loading…
Reference in a new issue