# Django Constants
This is a Django app designed to store global constants with different data types. It provides a central location to manage and access configuration values that can be easily reused throughout your Django project.
## Getting Started
1. Add "django_constants" to the INSTALLED_APPS list in your project's settings:
```
INSTALLED_APPS = [
# ...
"django_constants",
# ...
]
```
2. Apply migrations:
```
python manage.py migrate
```
## Usage
### Adding Global Constants
1. Log in to the Django admin interface.
Navigate to the "Global Constants" section.
Add new constants with their names, values, and data types.
2. Create/Update through models
```
from django_constants.models import GlobalConstant, KeyTypeChoices
GlobalConstant.objects.create(
key="CONSTANT_NAME", key_type=KeyTypeChoices.INT, value="123"
)
# NOTE: key is a unique field in database so only one key can exist in GlobalConstant model
GlobalConstant.objects.filter(key="CONSTANT_NAME").update(value="1234")
# Similary constants of other types can be created by specifying the key type and value as string
GlobalConstant.objects.create(
key="sample_str", key_type=KeyTypeChoices.STR, value="abc"
)
```
3. Sample create functions for all other available data types
```
GlobalConstant.objects.create(
key="sample_int", key_type=KeyTypeChoices.INT, value="123"
)
GlobalConstant.objects.create(
key="sample_float", key_type=KeyTypeChoices.FLOAT, value="123.345"
)
GlobalConstant.objects.create(
key="sample_bool", key_type=KeyTypeChoices.BOOL, value="True"
)
GlobalConstant.objects.create(
key="sample_dict",
key_type=KeyTypeChoices.DICT,
value="""{"abc":"def",1:"234","4":{"a":"b"}}""",
)
GlobalConstant.objects.create(
key="sample_list",
key_type=KeyTypeChoices.LIST,
value="""["123","ABC",12,14.567,True,False]""",
)
GlobalConstant.objects.create(
key="sample_tuple",
key_type=KeyTypeChoices.TUPLE,
value="""("123","ABC",12,14.567,True,False)""",
)
GlobalConstant.objects.create(
key="sample_set",
key_type=KeyTypeChoices.SET,
value="""{"123","ABC",12,14.567,True,False}""",
)
```
4. Accessing Global Constants
The get_constant_value method returns the constant value with the appropriate data type as specified during creation.
```
# Get the value of a constant
constant_value = GlobalConstant.objects.get_constant_value("CONSTANT_NAME")
# Output -> constant_value = 1234 (type <class 'int'>)
constant_value = GlobalConstant.objects.get_constant_value("sample_dict")
# Output -> constant_value = {"abc":"def",1:"234","4":{"a":"b"}} (type <class 'dict'>)
```
## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.
## v1.0.0
- First version of this Django constants!
- Validated against Django >= 3.2 .
- Support for data types such as str, int, float, bool, dict, list, tuple, set
## Next Milestones
- Add support for bytes datatype
- Add encryption logic for senstive constants like secret keys, payment related info, etc.
Raw data
{
"_id": null,
"home_page": "https://github.com/yashmarathe21/django-constants",
"name": "django-constant",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "django python constants",
"author": "Yash Marathe",
"author_email": "yashmarathe21@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3d/28/41035909acf627d2065b632cc8bae4da1bbacb978ca3e6f0bc4ee75e7609/django_constant-1.0.0.tar.gz",
"platform": null,
"description": "# Django Constants\n\nThis is a Django app designed to store global constants with different data types. It provides a central location to manage and access configuration values that can be easily reused throughout your Django project.\n\n## Getting Started\n\n1. Add \"django_constants\" to the INSTALLED_APPS list in your project's settings:\n```\nINSTALLED_APPS = [\n # ...\n \"django_constants\",\n # ...\n]\n```\n\n2. Apply migrations:\n```\npython manage.py migrate\n```\n\n## Usage\n\n### Adding Global Constants\n1. Log in to the Django admin interface.\nNavigate to the \"Global Constants\" section.\nAdd new constants with their names, values, and data types.\n\n2. Create/Update through models\n```\nfrom django_constants.models import GlobalConstant, KeyTypeChoices\n\nGlobalConstant.objects.create(\n key=\"CONSTANT_NAME\", key_type=KeyTypeChoices.INT, value=\"123\"\n)\n\n# NOTE: key is a unique field in database so only one key can exist in GlobalConstant model\nGlobalConstant.objects.filter(key=\"CONSTANT_NAME\").update(value=\"1234\")\n\n# Similary constants of other types can be created by specifying the key type and value as string\nGlobalConstant.objects.create(\n key=\"sample_str\", key_type=KeyTypeChoices.STR, value=\"abc\"\n)\n```\n\n3. Sample create functions for all other available data types\n\n```\nGlobalConstant.objects.create(\n key=\"sample_int\", key_type=KeyTypeChoices.INT, value=\"123\"\n)\n\nGlobalConstant.objects.create(\n key=\"sample_float\", key_type=KeyTypeChoices.FLOAT, value=\"123.345\"\n)\n\nGlobalConstant.objects.create(\n key=\"sample_bool\", key_type=KeyTypeChoices.BOOL, value=\"True\"\n)\n\nGlobalConstant.objects.create(\n key=\"sample_dict\",\n key_type=KeyTypeChoices.DICT,\n value=\"\"\"{\"abc\":\"def\",1:\"234\",\"4\":{\"a\":\"b\"}}\"\"\",\n)\n\nGlobalConstant.objects.create(\n key=\"sample_list\",\n key_type=KeyTypeChoices.LIST,\n value=\"\"\"[\"123\",\"ABC\",12,14.567,True,False]\"\"\",\n)\n\nGlobalConstant.objects.create(\n key=\"sample_tuple\",\n key_type=KeyTypeChoices.TUPLE,\n value=\"\"\"(\"123\",\"ABC\",12,14.567,True,False)\"\"\",\n)\n\nGlobalConstant.objects.create(\n key=\"sample_set\",\n key_type=KeyTypeChoices.SET,\n value=\"\"\"{\"123\",\"ABC\",12,14.567,True,False}\"\"\",\n)\n```\n\n4. Accessing Global Constants\n\nThe get_constant_value method returns the constant value with the appropriate data type as specified during creation.\n\n```\n# Get the value of a constant\nconstant_value = GlobalConstant.objects.get_constant_value(\"CONSTANT_NAME\")\n# Output -> constant_value = 1234 (type <class 'int'>)\n\nconstant_value = GlobalConstant.objects.get_constant_value(\"sample_dict\")\n# Output -> constant_value = {\"abc\":\"def\",1:\"234\",\"4\":{\"a\":\"b\"}} (type <class 'dict'>)\n```\n\n\n## Contributing\nContributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.\n\n## v1.0.0\n- First version of this Django constants!\n- Validated against Django >= 3.2 .\n- Support for data types such as str, int, float, bool, dict, list, tuple, set\n\n## Next Milestones\n- Add support for bytes datatype\n- Add encryption logic for senstive constants like secret keys, payment related info, etc.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "This is a Django app designed to store global constants with different data types. It provides a central location to manage and access configuration values that can be easily reused throughout your Django project.",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/yashmarathe21/django-constants"
},
"split_keywords": [
"django",
"python",
"constants"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3d2841035909acf627d2065b632cc8bae4da1bbacb978ca3e6f0bc4ee75e7609",
"md5": "7df7737d17b4f2d7d4eefa9c8ab86d91",
"sha256": "becbde30015995692a533e37dd537cd5d4c02e984c265e07dd5109572db90d1e"
},
"downloads": -1,
"filename": "django_constant-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "7df7737d17b4f2d7d4eefa9c8ab86d91",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5117,
"upload_time": "2023-08-06T13:06:23",
"upload_time_iso_8601": "2023-08-06T13:06:23.049489Z",
"url": "https://files.pythonhosted.org/packages/3d/28/41035909acf627d2065b632cc8bae4da1bbacb978ca3e6f0bc4ee75e7609/django_constant-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-06 13:06:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yashmarathe21",
"github_project": "django-constants",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-constant"
}