Niladry Kar
Niladry Kar

Reputation: 1203

How to remove the instance of ManyToMany field?

I have two models:

class Organisation(models.Model):
    user        = models.OneToOneField(settings.AUTH_USER_MODEL,related_name='organisation_user',on_delete=models.CASCADE)
    name        = models.CharField(max_length=100,blank=True)
    location    = models.CharField(max_length=100,blank=True)
    qualification_status = (
        ('Pending for verification','Pending for verification'),
        ('Verified','Verified'),
        )
    qualification   = models.CharField(max_length=100,choices=qualification_status,default='Pending for verification',blank=True)
    members         = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='organisation_members',blank=True)

class Organisation_member(models.Model):
    user            = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
    organisation    = models.ForeignKey(Organisation,on_delete=models.CASCADE,related_name='organisation_staff')
    member_name     = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='organisation_staff_member',on_delete=models.CASCADE,null=True,blank=True)
    is_admin        = models.BooleanField(default=False)

I have this signal for creation of Organisation_member model:

@receiver(post_save, sender=Organisation)
def organisation_admin(sender, instance, created, **kwargs):
    for member in instance.members.all():
        if Organisation_member.objects.filter(user=instance.user,organisation=Organisation.objects.filter(user=instance.user,name=instance.name).first(),member_name=member).exists():
            pass
        else:
            Organisation_member.objects.update_or_create(User=instance.User,organisation=Organisation.objects.filter(user=instance.user,name=instance.name).first(),member_name=member,is_admin=False)

The signal indicates that when I add a member in my manytomany field it will automatically create a Organisation_member object of the selected member.

The signal works perfectly fine.

My problem is the reverse i.e. when I try to delete an object of Organisation_member it should also remove the member from the manytomany relationship of the parent model.

I have tried this:

@login_required()
def delete_members(request, pk):
    user_organisation = get_object_or_404(Organisation, user=request.user)
    member_to_delete = Organisation_member.objects.filter(pk=pk)
    if member_to_delete.exists():
        member_to_delete[0].delete()
        for member in user_organisation.members.all():
            user_organisation.members.remove(member=member_to_delete[0])
    return redirect(reverse('userprofile:organisation_member_list'))

But it does not removes the member from the parent model..

Anyone who knows the solution please help.

Thank you

Upvotes: 0

Views: 254

Answers (2)

Endre Both
Endre Both

Reputation: 5740

The standard way of using this kind of relationship would be to specify Organisation_member as the through model for members in Organisation. Then the database takes care of it for you without any signals.

class Organisation(models.Model):
    members = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        through='Organisation_member')

The only problem is that you currently have two references to User in Organisation_member: member_name and user. Why? You probably only need one and should delete the other. If there is a reason for having two different users in Organisation_member, you need to specify the fields to use for the relationship:

    members = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        through='Organisation_member',
        through_fields=('organisation', 'member_name'))

That's it, Organisation_members are automatically added and removed whenever you add or remove a user to or from members.

Upvotes: 0

Atley Varghese
Atley Varghese

Reputation: 576

Try something like this:

@login_required()
def delete_members(request, pk):
    user_organisation = get_object_or_404(Organisation, user=request.user)
    member_to_delete = get_object_or_404(Organisation_member, pk=pk)
    user_organisation.members.remove(member_to_delete.member_name)
    member_to_delete.delete()
    return redirect(reverse('userprofile:organisation_member_list'))

Refer Django offical doc

Upvotes: 1

Related Questions