我按照文档了解如何在下面的 manytomanyfield 中删除一个项目,但它似乎不起作用,我收到了一个属性错误。remove 方法在没有中间模型的情况下工作得很好。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贡献1868条经验 获得超4个赞
您可以删除中间实例:
p = Playlist.objects.get(id=P_id)
s = Song.objects.get(id=id)
Playlist_Activity.objects.get(song=s, playlist=p).delete()
添加回答
举报
0/150
提交
取消