[Answer]-Include needed for Python/Django

1👍

The common way in django

I believe this is the closest to what you are asking. Any app you create in django can set up custom settings in the settings.py for users to configure. This allows easy distribution. Read here:
https://docs.djangoproject.com/en/1.4/topics/settings/#creating-your-own-settings

You set defaults, and document them. Then users can overload them in the settings.py of the project to customize the values.

The less common way in django, but maybe common from a general programming standpoint

In terms of “magic numbers”, I don’t think a database field property is really a magic number. Its usually something that is specific to the design of the model, but if you really don’t want to use the settings.py, and all you want is a constants module then just do something like this:

constants.py

MODEL1_FIELD1_MAXLENGTH = 100

models.py

import constants

a = CharField(max_length=constants.MODEL1_FIELD1_MAXLENGTH)

But honestly, I don’t see this happen very often. The value is usually coupled closely to the design and has an impact on other code that will use the model, like formatting situations and whatnot. Its kind of like allowing an end-user to set arbitrary values to internal buffers. It could impact code that expects a reasonable buffer because its part of the design decision. But I do understand the idea of constants from a general standpoint. Its just not that common in this specific framework, at this specific area.

Technically you could use a common format like JSON for your config file, but you would have to decide how and when it would decode. In django, the modules get loaded once and stay loaded through the life of the django instance. This might be something you init from the settings.py to ensure the order of operations.

config.json

{
    "MAX_LENGTH": 100
}

constants.py

import json

CONSTANTS = {}
CONSTANTS_FILE = "config.json"

def initConstants():
    global CONSTANTS_FILE
    with open(CONSTANTS_FILE) as f:
        CONSTANTS_FILE = json.load(f)
        print CONSTANTS_FILE
        # {"MAX_LENGTH": 100}
đŸ‘€jdi

0👍

Further to the comment, by jdi, you will need to add the max_length property to the CharField’s but what you can do is:

Add a property at the top of the models.py or any other “Common” file say:

MAX_LENGTH=100

Then, import Commpn.py

a = CharField(max_length=Common.MAX_LENGTH, ....)

Hope this helps you


đŸ‘€GHz

0👍

This isn’t rocket science.

Create a constants file, say constants.txt. Put name/value pairs in that file in an easily-parseable format. For example name:value. Write a small program in your language of choice (Python would be great for this). This program reads in constants.txt, and then writes out an appropriate file for each of the languages you will be working with (like constants.py, constants.h, etc.)

For example, if constants.txt contained an entry of ‘MODEL1_FIELD1_MAX_LENGTH: 20’, then constants.h could contain an entry of the form ‘#define MODEL1_FIELD1_MAX_LENGTH 20’, but constants.py would contain an entry of the form ‘MODEL1_FIELD1_MAX_LENGTH=20’. You get the picture.

Now make that little program be run automatically as part of your projects build process any time constants.txt is changed.

There you go–your constants kept in one file, yet always synchronized and available for any language you use.

đŸ‘€Michael Kent

Leave a comment