根据一个例子,我有三个模型:
class User(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(User, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(User)
group = models.ForeignKey(Group)
date_joined = models.DateField()
添加成员有效。但是如何删除单个成员资格实例(用户退出组),而不删除用户或组?
当我尝试像这样删除它时:
u = User(request.user)
g = Group.objects.get(id=group_id, membership__user=u)
m = Membership(user=request.user, group=g)
m.delete()
我收到一个错误:
/groups/quit/1/ 处的 AssertionError
无法删除成员资格对象,因为其 id 属性设置为 None。