如何仅删除 m2m 关系?

发布于 2024-10-21 04:55:40 字数 1072 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-10-28 04:55:40

多对多 管理器。

Province.objects.get(id=3).user.remove(user_id)

如果您愿意,您还可以直接访问 through 表:

Province.user.through.objects.get(province__id=3, user__id=4).delete()

Use the remove method on your ManyToMany manager.

Province.objects.get(id=3).user.remove(user_id)

You can also access the through table directly if you so desire:

Province.user.through.objects.get(province__id=3, user__id=4).delete()
肩上的翅膀 2024-10-28 04:55:40

如果您只需要删除两个模型之间所有实例的关系,那么您可以通过访问关系表的管理器来完成此操作。 m2m 关系表可以通过 MyModel.relations.through 访问,因此删除关系变得很容易:

MyModel.relations.through.objects.all().delete()

参考:

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:

MyModel.relations.through.objects.all().delete()

reference:

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through

掩耳倾听 2024-10-28 04:55:40

我知道这个问题很老了......
如果您想删除特定的所有用户

province.user.clear()

I know this question is old...
If you want to delete all the users of a specific province:

province.user.clear()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文