Configuration#
You can provide settings for django-user-trace by adding DJANGO_USER_TRACE as a dictionary to your Django settings.
The default settings are as follows:
settings.py
DJANGO_USER_TRACE = {
"USER_ATTRS": {"username": "get_username"},
"CELERY_TASK_HEADER": "User",
}
USER_ATTRS#
Default: {"username": "get_username"}
Type: dict[str, str | Callable[[AbstractBaseUser | AnonymousUser, HttpRequest], Any]]
This option controls which request.user attributes are made available to the log records.
It is a mapping of log record field names to either:
- an attribute on the Django
request.userobject; - a callable that accepts (
AbstractBaseUser|AnonymousUser,HttpRequest) and returns the result; - an import string (prefixed with
ext://) to a callable as seen in (2) above.
Tip
To lookup nested attributes, separate them by __ (two underscores), e.g. profile__country__code.
settings.py
def get_custom_attribute(user, request):
return f"Mr. {user.get_short_name()}"
DJANGO_USER_TRACE = {
# ...
"USER_ATTRS": {
"email": "email",#(1)!
"username": "get_username",#(2)!
"custom": get_custom_attribute,#(3)!
"custom_2": "ext://settings.get_custom_attribute",#(4)!
},
}
- Here, we are configuring the
USER_ATTRSoption ofdjango-user-traceto maprequest.user.emailto theusernamefield on a log record. - Here, we are mapping the result of invoking the
get_username()method on therequest.userinstance, to theusernamefield on a log record. - Here, we are mapping the result of invoking the
get_custom_attributecallable (defined above) to thecustomfield on a log record. - Here is another way of invoking the same
get_custom_attributecallable (defined above) by first importing the callable fromsettings.get_custom_attribute.
CELERY_TASK_HEADER#
Default: "User"
Type: str
This option determines the name of the Celery task header used to trace user attributes when django_user_trace.contrib.celery integration is installed.
Last update: 2023-07-02
Created: 2023-02-11
Created: 2023-02-11