How to remove relation from django model with ManyToMany field?
How to remove relation from model with ManyToMany field?
I've got a model with ManyToManyField relation. I need to remove relation but not data from the following model:
class TxHomes(models.Model): user = models.ManyToManyField(settings.AUTH_USER_MODEL) home_id = models.PositiveIntegerField(primary_key=True, unique=True, null=False) name = models.CharField(max_length=255, null=True) geo_name = models.CharField(max_length=255, null=True) payload = models.JSONField()
Django ORM got tables generated:
-- auto-generated definition create table main_txhomes ( home_id integer unsigned not null primary key, name varchar(255), geo_name varchar(255), ... ); create table main_txhomes_user ( id primary key autoincrement, txhomes_id ..., user_id ... );
When I apply to do that with a following code
TxHomes.objects.filter( home_id__in=TxHomes.objects.filter(user=USER_ID).values('home_id') ,user=USER_ID).delete()
i got entire data deleted from main_txhomes
I want to keep data in main_txhomes table, what i need is to delete relations from main_txhomes_user table. How to do that?
Solution is found:
This is how we remove all relation to txhomes table for the user