This contains utility like permissions, audit log etc.
Detailed documentation is in the "docs" directory.
-
Using pip:
pip install emtex_common_utils
or add
emtex_common_utils
in your requirements.txt file and then run:pip install -r requirements.txt
-
Add "emtex_common_utils" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'emtex_common_utils', ]
-
Database Migrations::
python manage.py emtex_migrate --for_date="YYYY-MM-DD"
-
Logging:
from emtex_common_utils.models import BaseModel, BaseLogModel from emtex_common_utils.mixins import BaseModelMixin ... class MyModelLog(BaseLogModel): """ Model to log changes for model MyModel """ pass class MyModel(BaseModel, BaseModelMixin): """ Model which changes needs to be tracked. """ class Meta: log_fields = ( my_field1, my_field2, ... ) log_model_name = MyModelLog
** Note To track foreign key, you need to pass the foreign key name or related_name if related_name is defined.
-
Handler functions inside
BaseModelMixin
that you can use to track changes:- obj.has_changed(field_name)
- obj.get_old_value(field_name)
- obj.get_new_value(field_name)
P.S.
obj
here is theMyModel
instance andfield_name
must be any field that is defined inlog_fields
in classMeta
of modelMyModel
. -
Run raw SQL query using sqlalchemy.
from emtex_common_utils.sqlalchemy_services import run_db_query sql_query = """ select field_name1, field_name2 from some_database.some_model where some_field = 'some value' """ results = run_db_query(sql_query)