Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

python - Django - objects.values() and prefetch_related() in same query

I have Book, Profile, Book_stat model as below. I am trying to minimize the fields from Book model and prefetch_related method to get the data from Book_stat model.

models.py

class Book(models.Model):
    title = models.CharField(max_length=200)
    image = models.FileField(upload_to="mp/", blank=True)

    def __str__(self):
        return self.title

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(max_length=150)

    def __str__(self):
        return self.full_name

class Book_stat(models.Model):
    user = models.ForeignKey(User)
    book = models.ForeignKey(Book)
    rating = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal('0.00'))
    like = models.BooleanField(default=False)

    def __str__(self):
        return self.book.title + ' by: ' + self.user.profile.full_name  

views.py

def books(request):
    books = books = Book.objects.prefetch_related('book_stat_set').values('id','title', 'image')
    for book in books:
        for book_stat in book.book_stat_set.all(): # error: 'dict' object has no attribute 'book_stat_set'
            book.stats = book_stat
            pprint(vars(book_stat))
    return HttpResponse(books, content_type= 'json')

1) I would like to send the json response to front end as below
2) If possible i want to use book_stat_set.values('like', 'rating') instead of book_stat_set.all() so that i query for like, rating only

books = [
    {
        id: 1,
        title: 'some title1',
        image: 'image1.jpg',
        stats: {
            like: true,
            rating: 3.50
        }
    },
    {
        id: 2,
        title: 'some title2',
        image: 'image1.jpg',
        stats: {
            like: true,
            rating: 3.50
        }
    }
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead the values method you can use the only and serialize manually:

from django.db.models import Prefetch
from django.forms import model_to_dict
from django.http import JsonResponse

def books(request):
    prefetch = Prefetch('book_stat_set', queryset=Book_stat.objects.only('like', 'rating'))
    qs = Book.objects.prefetch_related(prefetch).only('id','title', 'image')
    result = []
    for book in qs:
        row = model_to_dict(book, fields=['id','title', 'image'])
        row['image'] = row['image'].url
        stats = []
        for book_stat in book.book_stat_set.all():
            stat = model_to_dict(book_stat, fields=['like', 'rating'])
            stat['rating'] = str(stat['rating'])  # or float(stat['rating']) - depends on your purposes
            stats.append(stat)
        # you can calculate an average rating here
        row['stats'] = stats
        result.append(row)
    return JsonResponse(result)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...