[Solved]-AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'

8👍

I made an app with your code above and managed to recreate the issue.

I tried switching ‘self’ to ‘Employee’ as suggested here and tried tweaking a couple other things (like on_delete=models.CASCADE) in the field, but still instantiating an Employee object and calling .pm on it threw the error.

I think django has expectations about what classes you can use as the through parameter for a ManyToMany and it has to have two foreign keys, not a foreign key and a ManyToMany.

So…

If you switch to this:

class PM(models.Model):
    from_employee = models.ForeignKey(Employee, related_name='from_employee')
    to_employee = models.ForeignKey(Employee, related_name='to_employee')

it works. And that’s actually the normal pattern for ManyToMany relationships anyways — each PM represents a Project Manager relationship, not a person.

Alternatively,

You could have project manager be a foreign key from Employee to Employee, named something like managed_by to make sure each employee can only have one project manager.

Leave a comment