python - 如何在 Django View 中定义 get_queryset、get_context_data?

标签 python django django-models django-templates django-views

我想展示一棵树,其中包含 AuthorsBooks 为子项中的每个作者编写的, 喜欢show in the image和...我有两个模型 AuthorBook 在 OneToMany 关系中。

#models.py
from django.db import models

class Author(models.Model):
    Name = models.CharField(max_length = 250)

    def __unicode__(self):
        return self.Name

class Book(models.Model):
    Title = models.CharField(max_length = 250)

    def __unicode__(self):
        return self.Title


#views.py
from django.shortcuts import render, get_object_or_404
from django.views.generic import TemplateView, ListView

from .models import InstanciaJudicial, SedeJudicial

class Prueba(ListView):
    model = SedeJudicial
    template_name = 'instancias/pruebas.html'

我知道我定义了 get_querysetget_context_data,但我不知道我是怎么做到的。

最佳答案

首先,您需要在模型之间建立一个ForeignKey 关系。

#models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length = 250)
    author = models.ForeignKey(Author, related_name="books")

    def __unicode__(self):
        return self.Title

现在在您的 View 中,您应该能够通过覆盖 get_queryset 方法来检索您的作者列表,如下所示:

#views.py
from django.shortcuts import render, get_object_or_404
from django.views.generic import TemplateView, ListView

from .models import Author

class BooksByAuthorList(ListView):
    model = Book
    template_name = 'instancias/pruebas.html'

    def get_queryset(self):
        return Author.objects.prefetch_related("books").all()

只要上面的 View 你就应该能够在你的模板中拥有:

<ul>
{% for author in object_list %}
  <li>{{author.name}}</li><ul>
  {% for book in author.books.all %}
    <li>book.title</li>
  {% endfor %}
  </ul>
{% endfor %}
</ul>

现在假设您想对其进行自定义,这样上下文变量就不是通用的 object_list,而是像 authors 这样的领域中有意义的东西。

像这样扩充你的 View :

class BooksByAuthorList(ListView):
    model = Author
    template_name = 'instancias/pruebas.html'
    context_object_name = 'authors'        

    def get_queryset(self):
        return Author.objects.prefetch_related("books").all()

请注意,您根本不需要 get_context_data

假设您想要包含一些额外的数据,您只想覆盖 get_context_data 并且在这种情况下,您需要通过调用父类(super class)来保留已经在您的上下文中的对象列表get_context_data 方法优先。

只是做:

    def get_context_data(self, *args, **kwargs):
        # Call the base implementation first to get a context
        context = super(BooksByAuthorList, self).get_context_data(*args, **kwargs)
        # add whatever to your context:
        context['whatever'] = "MORE STUFF"
        return context

get_context_data 参数由您的路由决定。 *args**kwargs 可能应该替换为您的实际代码中特定于您的 View 和路由的内容。

关于python - 如何在 Django View 中定义 get_queryset、get_context_data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25277099/

相关文章:

python - Django - AttributeError : 'NoneType' object has no attribute 'first_name'

django - 如何从现场添加经理

python - 如何编辑 Django 网站页面上的内容?

html - 如何在 django 元素中使用下载的字体而不是 google fonts api

python - OpenERP 如何在many2one字段上使用域过滤器?

python - Python 中基于多行值填充列

python - 使用索引循环的最快方法

python - 成功执行 Django python 命令的*唯一*方法是使用 "python ...PATH...django-admin.py [options]"。为什么不能减少呢?

javascript - AJAX 和 DJANGO 形式

android - python 和 (android) adb shell