我使用中间模型创建了以下M2M表 -
class Position(models.Model):
position = models.CharField(max_length=100)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
positions = models.ManyToManyField(Position, through ='Timestamp', blank=True, null=True)
class Timestamp(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
position = models.ForeignKey(Position)
userprofile = models.ForeignKey(UserProfile)
到目前为止,我已创建了以下条目 -
>>> entry1 = Timestamp(userprofile=userprofile, position=position1)
>>> entry2 = Timestamp(userprofile=userprofile, position=position2)
现在我在userprofile_timestamp
表
-- profile & position1
-- profile & position2
如何删除profile & position1
条目?在文档(https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships)中,它提到使用profile.positions.clear()
清除所有条目,但是如何删除单个条目?谢谢。
答案 0 :(得分:0)
>>> profile.positions.through.objects.get(position=position1).delete()