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}
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âŠ
- [Answer]-KeyError from customized clean() method in BaseModelFormset
- [Answer]-Passing a model instance, not __unicode__ method in django
- [Answer]-Django; Will not make the correct request
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.
- [Answer]-API endpoint confusion
- [Answer]-Pycharm doesn't recognize Django installation
- [Answer]-Display django class based views data depending on user permissions?
- [Answer]-How do I register a class for the change_form.html in the admin template and use the key of the object?
- [Answer]-Not able to send email to more than 2 email address