我按照文檔了解如何在下面的 manytomanyfield 中刪除一個(gè)項(xiàng)目,但它似乎不起作用,我收到了一個(gè)屬性錯(cuò)誤。remove 方法在沒(méi)有中間模型的情況下工作得很好。models.py class Song (models.Model): author=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1) timestamp= models.DateTimeField(auto_now_add=True, editable=False) songname = models.CharField(max_length=500, null=False, blank=False) tags = TaggableManager() def __str__(self): return self.songname class Playlist (models.Model): name = models.CharField(max_length=150, null=False, blank=False) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1) song = models.ManyToManyField(Song, through='Playlist_Activity') class Playlist_Activity(models.Model): song= models.ForeignKey(Song, on_delete=models.CASCADE) playlist= models.ForeignKey(Playlist, on_delete=models.CASCADE) timestamp= models.DateTimeField(auto_now_add=True, editable=False) class Meta: ordering = ('timestamp',)Views.py@login_requireddef Playlist_Remove(request, id, P_id): p = Playlist.objects.get(id=P_id) s = p.song.get(id=id) p.song.remove(s) return redirect('account')
1 回答

MYYA
TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以刪除中間實(shí)例:
p = Playlist.objects.get(id=P_id)
s = Song.objects.get(id=id)
Playlist_Activity.objects.get(song=s, playlist=p).delete()
添加回答
舉報(bào)
0/150
提交
取消