简体   繁体   English

MonthArchiveView中的get_queryset()返回所有对象,而不是仅在请求的月份中创建的对象

[英]get_queryset() in MonthArchiveView returns all objects instead objects created only in requested month

Why get_queryset() from MonthArchiveView returns all objects from my model instead objects created only in requested month? 为什么get_queryset()返回模型中的所有对象,而不是仅在请求的月份中created对象?

class BudgetMonthlyView(MonthArchiveView):
    template_name = 'budget/monthly.html'
    model = FinanceData
    date_field = "created"
    make_object_list = False
    allow_future = False
    month_format = '%m'

    def get_context_data(self, **kwargs):
        context = super(BudgetMonthlyView, self).get_context_data(**kwargs)
        print(self.get_queryset()) #return all objects from FinanceData model
        print(context['object_list']) #works fine
        return context

It's just the way that the MonthArchiveView is implemented. 这就是MonthArchiveView的实现方式。 If you look at the source code , you can see that the object_list is returned by the get_dated_items method. 如果您查看源代码 ,则可以看到get_dated_items方法返回了object_list

It's probably implemented this way because the date archive views add other things to the context as well as the object_list , for example date_list . 之所以可以这样实现,是因为日期存档视图将其他内容以及object_list添加到上下文中,例如date_list

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

相关问题 如何在Django Admin中使用get_queryset仅检索AdminUser创建的对象? - How use get_queryset in Django Admin for retrieve only objects created by AdminUser? pk上的Django get_queryset过滤器对象 - Django get_queryset filter objects on pk 如何将两个模型对象传递给get_queryset视图? - How do I pass two model objects to a get_queryset view? 尽管在views.py中设置了get_queryset函数,但我的对象没有被过滤 - My objects are not getting filtered despite setting the get_queryset function in views.py Django MonthArchiveView在每月的1号返回错误的查询集(缓存?) - Django MonthArchiveView returning wrong queryset on the 1st of the month (caching?) 查询集只返回分配了外键的对象。Django - Queryset which returns only objects with a foreign key assigned.Django Django Model.objects.all()返回意外的空QuerySet - Django Model.objects.all() returns unexpected empty QuerySet 覆盖 get_queryset DetailView - override get_queryset DetailView 如果由QuerySet.as_manager()创建,则修改Django Manager get_queryset()方法 - Modify Django Manager get_queryset() method if created by QuerySet.as_manager() Django REST框架:重写get_queryset()有时会返回一个doubled查询集 - Django REST Framework: overriding get_queryset() sometimes returns a doubled queryset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM