Code

Opened 3 years ago

Closed 16 months ago

#17037 closed New feature (fixed)

Add management command to print active project settings

Reported by: msabramo Owned by: aaugustin
Component: Core (Management commands) Version: 1.3
Severity: Normal Keywords: printsettings
Cc: msabramo Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A large number of problems that we have with our Django project at work turn out to be because of settings that are not what they should be (note that our settings are not straightforward to see with a glance at settings.py since we use multiple settings files for production, dev, and common settings). I thought that it might be useful to have a Django management command that shows the settings in effect after all the settings files are processed.

It was super easy to write and is a tiny amount of code, but I have a feeling that this will quickly curtail a lot of problems.

Attachments (3)

django_manage_py_printsettings.diff (676 bytes) - added by msabramo 3 years ago.
Patch to add printsettings management command
django_manage_py_diffsettings_all.diff (1.3 KB) - added by msabramo 3 years ago.
Adds --all option to manage.py diffsettings
django_manage_py_diffsettings_all_v2.diff (1.3 KB) - added by msabramo 3 years ago.
Patch to add --all option to diffsettings management command

Download all attachments as: .zip

Change History (23)

Changed 3 years ago by msabramo

Patch to add printsettings management command

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

It seems to me that this already exists: manage.py diffsettings.

Did I miss something in your proposal?

comment:2 Changed 3 years ago by msabramo

  • Cc msabramo added

comment:3 Changed 3 years ago by msabramo

  • Resolution worksforme deleted
  • Status changed from closed to reopened

Sorry to reopen.

I didn't know about diffsettings. Thanks for pointing it out. Looks like it prints the settings that differ from Django core's default settings.

What my command does is slightly different. It prints out all the settings regardless of whether they are the same or different as the Django default.

So printsettings is simpler than diffsettings. I suppose both functions could be done by a single command (settings?) in order to minimize the # of commands, but I'll leave that question open. If folks want that, I can rework the patch.

comment:4 Changed 3 years ago by aaugustin

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

We could add an option to "diffsettings" to show the settings that are set to their default value too. They would be highlighted with a special marker, for instance ===.

I don't expect this option to be widely used — Django has a huge number of settings and in most cases the default is OK — but it can't hurt.

I think we should keep the name "diffsettings". The backwards compatibility issues outweigh the semantic improvement of changing it to "settings".

Changed 3 years ago by msabramo

Adds --all option to manage.py diffsettings

comment:5 Changed 3 years ago by msabramo

  • Patch needs improvement unset

See new patch django_manage_py_diffsettings_all.diff.

Some samples of use:

$ django diffsettings --all | grep '===' | head
ABSOLUTE_URL_OVERRIDES = {}  ===
ADMIN_FOR = ()  ===
ALLOWED_INCLUDE_ROOTS = ()  ===
APPEND_SLASH = True  ===
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)  ===
CACHES = {'default': {'LOCATION': '', 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}  ===
CACHE_MIDDLEWARE_ALIAS = 'default'  ===
CACHE_MIDDLEWARE_KEY_PREFIX = ''  ===
CACHE_MIDDLEWARE_SECONDS = 600  ===
COMMENTS_ALLOW_PROFANITIES = False  ===
$ django diffsettings --help | grep --after=2 -- '--all'
  --all                 Display all settings; not just ones that differ from
                        Django defaults. Settings that equal the defaults are
                        followed by "===".

comment:6 Changed 3 years ago by ptone

seems that this would be more suited as a contribution to django-extensions

comment:7 Changed 3 years ago by msabramo

  • Owner changed from nobody to aaugustin
  • Status changed from reopened to new

comment:8 follow-up: Changed 3 years ago by aaugustin

  • Owner changed from aaugustin to nobody

Please don't assign tickets to people without their consent.

comment:9 in reply to: ↑ 8 Changed 3 years ago by anonymous

Replying to aaugustin:

Please don't assign tickets to people without their consent.

Sorry. Was just trying to make sure you were in the loop on the patch that addressed your comment. I'll leave the assignments to you guys, though, since you know the protocol.

comment:10 Changed 3 years ago by aaugustin

By convention, people assign to themselves tickets they intend to work on.

I triaged this ticket — it's better than leaving it unreviewed — but it isn't very high on my priority list, so I didn't assign it to myself.

comment:11 follow-up: Changed 3 years ago by poirier

Instead of using "===" to mark default settings, how about instead prefixing those lines with "# "? Then the output will be a valid settings file, with the settings not changed from the defaults commented out, which might be useful.

comment:12 Changed 3 years ago by msabramo

  • Owner changed from nobody to msabramo

Assigning to myself so I can work on this.

Changed 3 years ago by msabramo

Patch to add --all option to diffsettings management command

comment:13 in reply to: ↑ 11 Changed 3 years ago by msabramo

Replying to poirier:

Instead of using "===" to mark default settings, how about instead prefixing those lines with "# "? Then the output will be a valid settings file, with the settings not changed from the defaults commented out, which might be useful.

That's a great idea! OK, see attachment:django_manage_py_diffsettings_all_v2.diff

comment:14 Changed 2 years ago by anonymous

@poirier: Did you get a chance to look at the latest patch in attachment:django_manage_py_diffsettings_all_v2.diff

comment:15 Changed 2 years ago by msabramo

Note that since this stalled, I created a separate Django app for this:

http://pypi.python.org/pypi/django-print-settings/

comment:16 Changed 18 months ago by aaugustin

  • Owner changed from msabramo to aaugustin
  • Status changed from new to assigned

comment:17 Changed 18 months ago by aaugustin

  • Needs tests set
  • Status changed from assigned to new

comment:18 Changed 16 months ago by aaugustin

  • Needs tests unset
  • Triage Stage changed from Accepted to Ready for checkin

I added tests and docs.

Final patch: https://github.com/django/django/pull/914

ptone: if you're strongly against adding this to Django, just close this ticket as wontfix -- I don't care much one way or another.

comment:19 Changed 16 months ago by claudep

I think this might be useful and does not add so much code, after all. +1

comment:20 Changed 16 months ago by Aymeric Augustin <aymeric.augustin@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 20a91cce04c72bc8c64a1c43b7398edac7b709cc:

Fixed #17037 -- Added a --all option to diffsettings.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.