[英]Django's Generic Views: how to filter get_queryset based on foreign class attributes?
[英]Django class based views, import inside get_queryset
我正在開發一個小應用程序,我正在使用基於類的視圖。 我在實現一個非常簡單的演示搜索功能時遇到了問題,出現以下錯誤:
Exception Type: AttributeError
Exception Value: type object'MyModel' has no attribute 'objects'
我通過在get_queryset中包含一個導入來解決這個問題 ,盡管我在文件的頂部有導入。 在下面找到一段示范代碼:
from mymodels.models import MyModel
class Search(generic.ListView):
"""Very simple search functionality."""
template_name = 'index.html'
context_object_name = 'object_list'
paginate_by = 5
def get_queryset(self):
from mymodels.models import MyModel
query = self.request.GET['search_text']
return MyModel.objects.filter(title__contains = query)
有誰知道為什么會這樣發生? 當我第一次使用基於類的視圖完成Django教程時,我沒有遇到這個問題。
我希望您在該文件中的其他位置重新分配名稱MyModel
:您可能在某處有MyModel = 'whatever'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.