如何删除只有m2m的关系?

时间:2022-12-29 14:38:59

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.

当我知道user_id和province_id时,如何删除关系?如果我用户delete()方法,它也删除省,我想避免它。我找不到任何地方如何删除m2m字段中的1个特定关系。

3 个解决方案

#1


10  

Use the remove method on your ManyToMany manager.

在ManyToMany管理器上使用remove方法。

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()

#2


1  

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:

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

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

reference:

参考:

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

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

#3


1  

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

我知道这个问题已经过时了...如果你想删除特定省份的所有用户:

province.user.clear()

#1


10  

Use the remove method on your ManyToMany manager.

在ManyToMany管理器上使用remove方法。

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()

#2


1  

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:

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

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

reference:

参考:

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

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

#3


1  

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

我知道这个问题已经过时了...如果你想删除特定省份的所有用户:

province.user.clear()