如何在Django的DetailView中显示查询?

huangapple go评论56阅读模式
英文:

How to display a query on DetailView in Django?

问题

我想让我的 "CustomDetailView" 显示一个查询(一个单独的 "flashcard")。我能够通过使用 ListView 来实现它。

CustomListView(ListView):
    model = Flashcard
    template_name = 'flashcards.html'
    queryset = Flashcard.objects.all()[:1]

但是对于 DetailView,我遇到了以下错误:

通用详细视图 CustomDetailView 必须在 URLconf 中使用对象的主键(pk)或 slug 中的一个进行调用。

class CustomDetailView(DetailView):
    model = Flashcard 
    template_name = "flashcards.html"
    queryset = Flashcard.objects.all().first()

urls.py:

path('', CustomDetailView.as_view(), name='flashcards')

如何修复这个问题?

英文:

I want my "CustomDetailView" to display a query(a single "flashcard"). I was able to it by using ListView

CustomListView(ListView):
    model = Flashcard
    template_name = 'flashcards.html
    queryset = Flashcard.objects.all()[:1]

But for DetaiView I'm getting this error
Generic detail view CustomDetailView must be called with either an object pk or a slug in the URLconf.

class CustomDetailView(DetailView):
    model = Flashcard 
    template_name = "flashcards.html"
    queryset = Flashcard.objects.all().first()

urls.py

path('', CustomDetailView.as_view(), name='flashcards'),

How to fix this?

答案1

得分: 3

从查询集中删除 first

class CustomDetailView(DetailView):
    model = Flashcard 
    template_name = "flashcards.html"
    queryset = Flashcard.objects.all().exclude(first=True)
    lookup_field = 'pk'
    lookup_url_kwarg = 'pk'

并将 id 添加到 URL:

path('<int:pk>/', CustomDetailView.as_view(), name='flashcards'),
英文:

remove first from queryset:

class CustomDetailView(DetailView):
    model = Flashcard 
    template_name = &quot;flashcards.html&quot;
    queryset = Flashcard.objects.all()
    lookup_field = &#39;pk&#39;
    lookup_url_kwarg = &#39;pk&#39;
    enter code here

and add id to url:

path(&#39;/&lt;int:pk&gt;/&#39;, CustomDetailView.as_view(), name=&#39;flashcards&#39;),

答案2

得分: 0

以下是您需要的翻译部分:

  • You need your pk or slug in:
    您需要在以下位置使用pkslug

  • See an example from the docs:
    请查看文档中的示例:

  • This is because DetailView is inheriting from SingleObjectMixin, and it fetches objects that way. If you want, jump to definition of this class in your IDE to see its implementation of get_queryset and get_object.
    这是因为DetailView继承自SingleObjectMixin,并通过这种方式获取对象。如果您愿意,可以在您的IDE中跳转到此类的定义,以查看其get_querysetget_object的实现方式。

  • How you call your pk or slug really depends on your model, the Flashcard. If your primary_key field is an Integer, I think you'll be fine writing:
    如何调用您的pkslug取决于您的模型,即Flashcard。如果您的primary_key字段是一个Integer,我认为您可以这样编写:

  • edit: as Pourya Mansouri wrote, you do need to remove queryset attribute in your case too.
    编辑: 正如Pourya Mansouri写的,在您的情况下,您确实需要删除queryset属性。

  • The slug you can customize with slug_field and similar attributes.
    您可以使用slug_field和类似的属性来自定义slug

  • You can define a completely custom object fetching behavior by overriding get_object or get_queryset. Here's a random examples of doing that I found on the internet:
    您可以通过覆盖get_objectget_queryset来定义完全自定义的对象获取行为。以下是我在互联网上找到的一些示例:

英文:

You need your pk or slug in

path(&#39;&#39;, CustomDetailView.as_view(), name=&#39;flashcards&#39;),

See an example from the docs:

urlpatterns = [
    path(&#39;&lt;slug:slug&gt;/&#39;, ArticleDetailView.as_view(), name=&#39;article-detail&#39;),
]

This is because DetailView is inheriting from SingleObjectMixin, and it fetches objects that way. If you want, jump to definition of this class in your IDE to see its implementation of get_queryset and get_object.

How you call your pk or slug really depends on your model, the Flashcard. If your primary_key field is an Integer, I think you'll be fine writing

path(&#39;&lt;int:pk&gt;&#39;, CustomDetailView.as_view(), name=&#39;flashcards&#39;),

edit: as Pourya Mansouri wrote, you do need to remove queryset attribute in your case too

The slug you can customize with slug_field and similar attributes.

You can define a completely custom object fetching behavior by overriding get_object or get_queryset. Here's a random examples of doing that I found on the internet:

答案3

得分: 0

也许应该更好地沟通。对不起我的英语。
当我点击链接并显示在detailView上时,我想返回一个随机查询。我通过以下方式实现了它。不认为它很高效。如果有人有任何想法,请分享。

def get_object(self):
    queryset = Flashcard.objects.order_by('?').first()
    return queryset
英文:

Maybe should have better communicated. Sorry for my English.
I wanted to return a random query when I clicked on a link a show it on detailView. I was able to it by this. Don't think it's efficient. If anyone has any idea share it.

def get_object(self):
    queryset = Flashcard.objects.order_by(&#39;?&#39;).first()
    return queryset 

huangapple
  • 本文由 发表于 2023年2月10日 14:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定