如何仅删除 m2m 关系?
model:
class Province(models.Model):
user = models.ManyToManyField(User, blank=True)
name = models.CharField(max_length=30, unique=True)
class City(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, editable=False, unique=False)
ownership = models.ManyToManyField(User, through='UserCity')
class UserCity(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
city = models.ForeignKey(City)
class District(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True, editable=False)
ownership = models.ManyToManyField(User, through='UserDistrict')
class UserDistrict(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
city = models.ForeignKey(City)
district = models.ForeignKey(District)
当我知道user_id和province_id时,如何删除关系?如果我使用 delete() 方法,它也会删除省份,我想避免它。我在任何地方都找不到如何删除 m2m 字段中的 1 个特定关系。
model:
class Province(models.Model):
user = models.ManyToManyField(User, blank=True)
name = models.CharField(max_length=30, unique=True)
class City(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, editable=False, unique=False)
ownership = models.ManyToManyField(User, through='UserCity')
class UserCity(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
city = models.ForeignKey(City)
class District(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True, editable=False)
ownership = models.ManyToManyField(User, through='UserDistrict')
class UserDistrict(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
city = models.ForeignKey(City)
district = models.ForeignKey(District)
How can I delete relation when I know user_id and province_id? If i user delete() method it also removes province and I want to avoid it. I can't find anywhere how to delete 1 specific relation in m2m field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 多对多 管理器。
如果您愿意,您还可以直接访问 through 表:
Use the remove method on your ManyToMany manager.
You can also access the through table directly if you so desire:
如果您只需要删除两个模型之间所有实例的关系,那么您可以通过访问关系表的管理器来完成此操作。 m2m 关系表可以通过 MyModel.relations.through 访问,因此删除关系变得很容易:
参考:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models。 ManyToManyField.through
If you need to delete only the relationship for all instance between 2 models then you can do that by accessing the Manager of the relationship table. The m2m relationship table can be accessed via
MyModel.relations.through
so for deleting the relationships it becomes easy:reference:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through
我知道这个问题很老了......
如果您想删除特定
省
的所有用户
:I know this question is old...
If you want to delete all the
users
of a specificprovince
: