Django:对象没有属性_set'

时间:2017-10-10 22:41:17

标签: python django

我在django项目中有这段代码:

def all_songs(request, filter_by):
    if not request.user.is_authenticated():
        return render(request, 'music/login.html')
    else:
        try:
            song_ids = []
            for album in Album.objects.all():
                for song in album.song_set.all():
                    song_ids.append(song.pk)
            users_songs = Song.objects.filter(pk__in=song_ids)
            for album in GenericAlbum.objects.all():
                for song in album.genericsong_set.all():
                    song_ids.append(song.pk)
            generic_songs = GenericSong.objects.filter(pk__in=song_ids)
        except Album.DoesNotExist:
            users_songs = []
        except GenericAlbum.DoesNotExist:
            generic_songs = []
        return render(request, 'music/songs.html', {
            'song_list_all': users_songs,
            'generic_song_list': generic_songs,
            'filter_by': filter_by,
        })

当我转到allSong页面时,我收到此错误:

  

AttributeError:' NoneType'对象没有属性' split'

我认为一个变量允许一个无值,但我不知道一个,我尝试了所有这些并且仍然是同样的问题。

这是追溯:

Traceback (most recent call last):
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\servers\basehttp.py", line 99, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 696, in __init__
    self.handle()
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle
    handler.run(self.server.get_app())
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

1 个答案:

答案 0 :(得分:1)

尝试一下,你尝试从pk对象获取queryset,但是pk它是实例的属性而不是queryset,当你需要来自queryset的值列表时,你可以使用{{3} },并为您的filter_by

添加默认值
def all_songs(request, filter_by=None):
    if not request.user.is_authenticated():
        return render(request, 'music/login.html')
    else:
        song_ids = []
        for album in Album.objects.all():
            song_ids.append(album.song_set.values_list('pk', flat=True))
        users_songs = Song.objects.filter(pk__in=song_ids)
        song_ids = []
        for album in GenericAlbum.objects.all():
            song_ids.append(album.genericsong_set.values_list('pk', flat=True))
        generic_songs = GenericSong.objects.filter(pk__in=song_ids)

        return render(request, 'music/songs.html', {
            'song_list_all': users_songs,
            'generic_song_list': generic_songs,
            'filter_by': filter_by,
        })