Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot delete instance of model referenced by ManyToManyField

See original GitHub issue

I have the following models:

class School(TenantModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100)
    tenant_id = 'id'

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    objects = UserManager()

    USERNAME_FIELD = 'email'

class UserProfile(TenantModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='userprofiles')
    school = models.ForeignKey(School, on_delete=models.CASCADE, related_name='members')
    departments = models.ManyToManyField('Department', blank=True, related_name="members")
    is_admin = models.BooleanField(default=False)
    tenant_id = 'school_id'

class Department(TenantModel):
    name = models.CharField(max_length=500, unique=True)
    school = models.ForeignKey(School, on_delete=models.CASCADE)

    tenant_id = 'school_id'

Attempting to delete a Department instance throws the error “TypeError: related_objects() takes 3 positional arguments but 4 were given”. This happens even if a tenanted through model is used.


  File "/Users/alex/Library/Mobile Documents/com~apple~CloudDocs/Documents/bunsen/backend/schools/tests/", line 171, in test_delete_department_successful
    response = self.client.delete(
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 317, in delete
    response = super().delete(
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 219, in delete
    return self.generic('DELETE', path, data, content_type, **extra)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 231, in generic
    return super().generic(
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/test/", line 470, in generic
    return self.request(**r)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 283, in request
    return super().request(**kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 235, in request
    request = super().request(**kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/test/", line 709, in request
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/test/", line 571, in check_exception
    raise exc_value
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/core/handlers/", line 47, in inner
    response = get_response(request)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/core/handlers/", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/views/decorators/", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 114, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 465, in handle_exception
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 476, in raise_uncaught_exception
    raise exc
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 91, in destroy
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/rest_framework/", line 95, in perform_destroy
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/db/models/", line 943, in delete
    collector.collect([self], keep_parents=keep_parents)
  File "/Users/alex/.local/share/virtualenvs/backend-xQfT061F/lib/python3.8/site-packages/django/db/models/", line 313, in collect
    sub_objs = self.related_objects(related_model, related_fields, batch)
TypeError: related_objects() takes 3 positional arguments but 4 were given```

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

louiseGrandjonccommented, Aug 25, 2020

The through model is not essential. It’s required for distributed databases though. So if you decided to shard your database on the column school_id and wanted to distribute the members relation. If you’re not using a distributed database, then it works perfectly fine 😃

pickyuptruckcommented, Aug 24, 2020

one final question: are through models essential? Things seem to be working fine without them

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deny deletion of model if it is referenced in a ManyToMany ...
Simplest approach will be to override the delete() method of Meal model. class Meal(models.Model): name_text = models.
Read more >
Delete a referenced object in a ManyToMany field-django
Model ) relatedObjects = models.ManyToManyField(RelatedModel). Above is a one of my models that references another model. I want to be able to delete...
Read more >
How to delete a record in many to many relationship? : r/django
ManyToMany field has some methods that you can call: add(Object). remove(Object). clear() this one is for removing ...
Read more >
Many-to-many relationships - Django documentation
To define a many-to-many relationship, use ManyToManyField . In this example, an Article can be published in multiple Publication objects, and a Publication ......
Read more >
Tips for Using Django's ManyToManyField - REVSYS
A primary key doesn't exist until a model instance is saved, so that's why both instances have to exist before they can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found