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.
Use the remove method on your ManyToMany manager.
You can also access the through table directly if you so desire: