Implementing Custom Model Defaults in the Django Admin Interface

I’ve been working on a project and need to seek some guidance regarding implementing custom model defaults in the Django admin interface. I want to allow administrators to modify certain default values effortlessly.

Currently this requires changing the actual code, which is not ideal. For example, a value in settings.py.

Specifically, I’m looking to have the ability to change the maximum file size and manage a set of retention times (represented as timedeltas) directly from the admin web view. However, I’m not entirely sure about the best approach for implementing this feature. Being able to toggle the REGISTRATION_OPEN setting in the web view would also be useful.

I’d appreciate it if anyone could point me in the right direction or share their experiences with a similar requirement. Are there any resources or documentation available that I can read?

It’s less a question of what you’re trying to set here and more a question of how those values are used. Not all “default values” are used in the same place by the same components, so a full answer to your question would require you to identify all values you’re looking to set and where they’re used throughout your system.

For example, values set in your settings.py file are not going to be adjustable within the context of the system. The settings file is processed before your application is initialized. By the time you get to your code, it’s too late to make those adjustments.

On the other hand, if the retention times are values used in a call to an external API, then you could change that value before the API call is made.

So there’s not going to be a general “one-size-fits-all” answer to your question. It can only be accurately addressed on a case-by-case basis.

(Side note: Changing the maximum file size for an upload has multiple implications beyond Django itself. It may also require changes to your web server - and possibly other components as well.)

1 Like