每次我 运行 代码时,我都会收到以下错误,我不知道出了什么问题

I am getting the error below every time I run the code and I do not know what is wrong

我收到以下错误。任何帮助表示赞赏。我可能从文档中遗漏了一些非常重要的东西。

如果你看到它请指出我的错误并启发我关于 Django 的多对多关系。从出现的错误来看,这是否意味着 .add() 函数不能用于查询集?

回溯

Internal Server Error: /7/
Traceback (most recent call last):
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\music\songs\views.py", line 141, in Playlist_Add
    playlist.song.add(song)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related_descriptors.py", line 926, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1073, in _add_items
    '%s__in' % target_field_name: new_ids,
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1225, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1096, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related_lookups.py", line 59, in get_prep_lookup
    self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related_lookups.py", line 59, in <listcomp>
    self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'QuerySet'
[16/Apr/2020 23:06:55] "GET /7/ HTTP/1.1" 500 127314

models.py

class Playlist (models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
    song = models.ManyToManyField(Song)

views.py

def Playlist_Add(request, id):
    song = Song.objects.filter(id=id)
    playlist, created = Playlist.objects.get_or_create(user=request.user)
    #playlist.save()
    playlist.song.add(song)
    return redirect('home:home')

urls.py

path('<int:id>/', views.Playlist_Add, name='playlist_add'),

模板

<footer class="w3-container w3-blue">
    <a href="{% url 'playlist_add' id=obj.id %}">
    <button type="button" class="btn btn-info">+ To Playlist</button>
    </a>
    <h5>last edited:&nbsp {{objects.timestamp}}</h5>
  </footer>

您的 song 不是一首歌曲,而是 QuerySet 首歌曲,因此可以包含零首、一首或多首歌曲。因为你过滤一个主键,它只能包含零个或一个Song,但无论如何,它仍然是一个集合。

您可以通过在 .filter() 上使用 .get(..) 或更好的 get_object_or_404(..):

来解决此问题
from django.shortcuts import <b>get_object_or_404</b>

def Playlist_Add(request, id):
    song = <b>get_object_or_404(Song, id=id)</b>
    playlist, created = Playlist.objects.get_or_create(user=request.user)
    #playlist.save()
    playlist.song.add(song)
    return redirect('home:home')

Note: A GET request is not supposed to have side-effects, hence constructing objects when a user makes a GET request, is not compliant with the HTTP standard. Therefore it might be better to turn this into a POST request.