0

I am trying to load a fixture during a django test case, but m2m_changed signal kicks in and it gives an error (the signal checks that a foreignkey is in a related state).

This answer suggests using the disable_for_loaddata decorator, but m2m_changed doesn't have a raw field.

I have tried:

class DaysTests(APITestCase):
    fixtures = ['initial_data.json'] # fixture is loaded before disabling m2m_changed
    def setUp(self):
        m2m_changed.disconnect(days_handler, sender=Foo.days.through)

    def test_api(self):
        # test logic.

Initial data is loaded before disabling the m2m_changed signal.

What is the right way to disconnect/disable a m2m_changed signal?

Community
  • 1
  • 1
Netro
  • 7,119
  • 6
  • 40
  • 58

1 Answers1

2

You should to try to disable this signal not in setUp method but in setUpClass because it's executed before loading fixtures. This is a possible way to use it:

@classmethod
def setUpClass(cls):
    super(DaysTests, cls).setUpClass()
    m2m_changed.disconnect(...)
Nick
  • 1,134
  • 7
  • 12