如何在Django获得同一作者的书籍

如何在Django获得同一作者的书籍,django,django-queryset,Django,Django Queryset,我正在开发一个在线书店。 以下是模型: class Author(models.Model): name = models.CharField(max_length=250, unique=True) class Publisher(models.Model): name = models.CharField(max_length=250, unique=True) class Book(models.Model): author = models.ManyT

我正在开发一个在线书店。 以下是模型:

class Author(models.Model):
    name = models.CharField(max_length=250, unique=True)

class Publisher(models.Model):
    name = models.CharField(max_length=250, unique=True)
    
class Book(models.Model):
    author = models.ManyToManyField(Author, related_name='authors')
    publisher = models.ForeignKey(Publisher, on_delete=models.PROTECT, blank=True, null=True)   
    isbn13 = models.BigIntegerField(unique=True)
    name = models.CharField(max_length=500)
    ... 
以下是视图:

class AuthorsListView(ListView):
    model = Author
    context_object_name = 'authors_list'
    template_name = 'authors_list.html'
    paginate_by = 500

class AuthorBooksListView(ListView):
    model = Book
    context_object_name = 'author_books'
    template_name = 'author_books.html'

    def get_queryset(self, **kwargs):
        author_id = Author.objects.get(pk = self.kwargs['pk'])
        qs = super().get_queryset(**kwargs)
        return qs.filter(author = author_id)


    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet 
        context['author'] = Author.objects.get(pk = self.kwargs['pk'])
        return context

class PublishersListView(ListView):
    model = Publisher
    context_object_name = 'publishers_list'
    template_name = 'publishers_list.html'
    paginate_by = 500

class PublisherBooksListView(ListView):
    model = Book
    context_object_name = 'publisher_books'
    template_name = 'publisher_books.html'
    paginate_by = 20

    def get_queryset(self, **kwargs):
        publisher_id = Publisher.objects.get(pk = self.kwargs['pk'])
        qs = super().get_queryset(**kwargs)
        return qs.filter(publisher = publisher_id)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet 
        context['publisher'] = Publisher.objects.get(pk = self.kwargs['pk'])
        return context

class BooksListView(ListView):
    model = Book
    context_object_name = 'books_list'
    template_name = 'books_list.html'
    paginate_by = 100


class BookDetailView(DetailView):
    model = Book
    template_name = 'book_detail.html'
以下是网址:

    path('authors/', AuthorsListView.as_view(), name = 'authors_list'),
    path('author/<int:pk>/', AuthorBooksListView.as_view(), name='author_detail'),
    path('publishers/', PublishersListView.as_view(), name='publishers_list'),
    path('publisher/<int:pk>/', PublisherBooksListView.as_view(), name='publisher_detail'),
    path('', BooksListView.as_view(), name='books_list'),
    path('book/<int:pk>/', BookDetailView.as_view(), name='book_detail'),
但这给了我一个错误:

'dict' object has no attribute 'filter'

@djangodjarhes-你能试试下面的吗?理想情况下,如果我想更改除URL kwargs之外的查询集过滤方式,我会修改get_queryset。对于其他内容,我将覆盖get\u context\u数据

  def get_context_data(self, **kwargs):
        context = super(BookDetailView, self).get_context_data(**kwargs)
        book =  Book.objects.get(pk = self.kwargs['pk'])
        publisher_books = Book.objects.filter(publisher = book.publisher.id)
        context["publisher_books"] = publisher_books
        return context
如果是你的情况,请随时纠正我

 queryset = {
                'books': Book.objects.all(),
                'publisher_books': Book.objects.filter(publisher = book.publisher.id),
        }
这是不对的。当get_queryset应该返回queryset时,您正在返回dict。您不能返回dict。您可以更改它以执行此操作

queryset = Book.objects.filter(publisher = book.publisher.id)
return queryset

如果您想返回口述,请使用“获取上下文”数据。

谢谢您的回答。你告诉我“如果我想改变查询集的过滤方式而不是URL kwargs,那么我最好修改get_查询集”,那么,在我的情况下,我如何修改get_查询集?@djangodjames-检查我的更新答案非常感谢。你的回答对我帮助很大。关于“当get_queryset应该返回一个queryset时,你正在返回一个dict”:我在这里读到了这个解决方案——Pran Kumar Sarkar的回答。据我所知,这是不对的?那么,如果需要,我如何获得多个查询集?还有一个问题。出版商的书很好用。但是我怎样才能得到作者的书呢?我的模型中的作者是多对多领域的。所以我不能使用Book.objects.filter(author=Book.author.id)?你能帮我做这个吗?
queryset = Book.objects.filter(publisher = book.publisher.id)
return queryset