sunscrapers/djoser

explain

brahim024 opened this issue · 1 comments

value = getattr(self, setting_name, {})

I'm a bit unclear on the context, but it sounds like you're trying to retrieve setting names from the object itself (self) rather than from the overridden settings dictionary.
can someone exaplain that please

I wasn't here when it was created and it looks weird indeed, but after looking deeper into it I think it's brilliant.

djoser/djoser/conf.py

Lines 121 to 124 in f3fc911

overriden_settings = (
getattr(django_settings, DJOSER_SETTINGS_NAMESPACE, {})
or explicit_overriden_settings
)

This allows us to obtain the settings either from django settings, or whenever they are updated (override_settings decorator etc) using the signal. It's just a dict.

value = getattr(self, setting_name, {})

I assume this case you're asking about is important only on update (whenever the signal is fired up), as this allows us to update the dictionary that's already set on self.

In the next line, it updates the existing dict (value) with these new values.

https://github.com/sunscrapers/djoser/blob/f3fc9110947af2afbd8325a1ac4350f0fa8a4908/djoser/conf.py#L140C17-L140C53

This allows us to import Djoser settings directly and use it within the Djoser project.

https://github.com/search?q=repo%3Asunscrapers%2Fdjoser+%22+settings.%22+path%3A*.py&type=code

Here, settings, is literally the instance of this Settings class, with attributes set with setattr onto it.

DRF uses a similar approach. See https://github.com/encode/django-rest-framework/blob/1db19f4b2d1500894448634852e02f73043382e4/rest_framework/settings.py#L197-L198