简体   繁体   English

Django ListView:“this_object_id”未定义

[英]Django ListView: 'this_object_id' is not defined

I keep having issues when I want to refer to a specific object in ListView .当我想在ListView中引用特定的 object 时,我一直遇到问题。

My views:我的看法:

class SongList(generic.ListView):
    model = models.Song
    template_name = 'videos/song_list.html'
    context_object_name = 'song_list'

    def get_context_data(self, **kwargs):
        context = super(SongList, self).get_context_data(**kwargs)
        user_flash = Flashcard.objects.filter(owner=self.request.user).values_list('question', flat=True)
        song = models.Song.objects.get(pk=this_object_id)  #works if hard-coded
        lyrics_list = models.Song.objects.get(song=song).lyrics_as_list()
        user_word = list(set(user_flash) & set(lyrics_list))
        context['percent_known'] = (len(user_word)/len(set((lyrics_list))))*100
        return context

I get the following error: name 'this_object_id' is not defined我收到以下错误: name 'this_object_id' is not defined

I've tried various variations, but I keep getting errors, eg我尝试了各种变化,但我不断收到错误,例如

self.kwargs['pk']
self.kwargs.get['pk']

Then I get the error: KeyError 'pk'然后我得到错误: KeyError 'pk'

It works when I hard-code the pk, so the problem is not elsewhere my code.当我对 pk 进行硬编码时它可以工作,所以问题不在我的代码的其他地方。 It also works when I do it in my DetailView (using self.kwargs['pk'] ), but I need access to percent_known for every object in my ListView .当我在我的DetailView中执行此操作时(使用self.kwargs['pk'] ),它也可以工作,但我需要访问我ListView中每个percent_known的 percent_known 。 How can I do this?我怎样才能做到这一点? I can sort of understand why it doesn't work, ListView is for a list of objects, so it doesn't know for what object I want to get this info for... but there must be a way I can do it for every object?我可以理解为什么它不起作用, ListView是一个对象列表,所以它不知道我想要获取此信息的原因是什么 object...每个 object? Or is there another way to get access to percent_known for every object?还是有另一种方法可以访问每个percent_known的 percent_known ?

The main issue here is that in the get_context_data() method you are trying to access information for a single item, when you are actually managing a list.这里的主要问题是,在get_context_data()方法中,当您实际管理列表时,您正试图访问单个项目的信息。

If you have some difficult calculations to perform for every object of your list, you can access those objects in the get_context_data() , like:如果您要对列表中的每个 object 执行一些困难的计算,您可以在get_context_data()中访问这些对象,例如:

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        for song in context['song_list']:
             # Here you already have the current Song object you want to process
             lyrics_list = models.Song.objects.get(song=song).lyrics_as_list()

             # and so on...
        
        return context

Another more efficient option would be to perform this calculations using the Django ORM system, annotating the Song s list under the get_queryset() method.另一个更有效的选择是使用 Django ORM 系统执行此计算,在get_queryset()方法下注释Song的列表。

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

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