简体   繁体   English

如何在django视图中定义get_queryset,get_context_data?

[英]How to define get_queryset, get_context_data in a django view?

I want show a tree with Authors and Books written for each Author in sub item, like show in the image and... I have two models Author and Book in a OneToMany relationship. 我希望在子项目中为每个作者显示一个带有AuthorsBooks的树,如图像中的显示和......我在OneToMany关系中有两个模型AuthorBook

#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'

I know that I defined get_queryset and get_context_data , but I don't know how I did this. 我知道我定义了get_querysetget_context_data ,但我不知道我是怎么做到的。

First of all you need to establish a ForeignKey relationship between your models. 首先,您需要在模型之间建立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

Now in your view you should be able to retrieve your list of authors by overriding the get_queryset method like this: 现在在您的视图中,您应该能够通过覆盖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()

With just the above view you should be able to have in your template: 只需上面的视图,您就可以在模板中使用:

<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>

Now say you want to customize that so that instead of the generic object_list the context variable was instead something sensible in the domain like authors . 现在说你要自定义它,这样代替通用的object_list ,上下文变量就像authors那样在域中是明智的。

Just augment your view like this: 只需增加您的视图:

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()

Note you don't need get_context_data at all yet. 请注意,您根本不需要get_context_data

Assuming you'd like to include some additional data you'll just want to override get_context_data and in this case you'll want to keep the object list that's already in your context by calling the superclasses get_context_data method first. 假设您想要包含一些额外的数据,您只想覆盖get_context_data ,在这种情况下,您将希望通过首先调用超类get_context_data方法来保留已经在您的上下文中的对象列表。

Just do: 做就是了:

    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 arguments are determined by your routes. get_context_data参数由您的路由决定。 *args and **kwargs should probably be replaced with something specific to your view and route in your actual code. *args**kwargs可能应该替换为您的视图中特定的内容并在实际代码中路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM