Djagno override_settings decorator not respected by DRF Serializer

In my Django settings I've defined a settings var like


My test tries to override this by use of override_settings decorator

def test_serializer_metadata_deferred_behavior(self):
    serializer = ResourceBaseSerializer(self.layer)
    metadata_field = vars(serializer.__class__)['_declared_fields'].get('metadata')
    self.assertEqual(metadata_field.deferred, True)

When I check the var in ResourceBaseSerializer

if getattr(settings, "EXTRA_METADATA_ENABLED", False):
    print("I am here")
    print("I am there")
    print(settings.EXTRA_METADATA_ENABLED) # False

It prints

I am there. 

Where I would expect Dog. Can someone shed light into what is going on? From my understanding the Serializer class should respect the settings var?

If your class ResourceBaseSerialzer has any critical method itself accessing settings then the override_settings decorator would have no effect at all. You may look at this blog post.

Back to Top