Add history + more components
This commit is contained in:
@@ -157,10 +157,8 @@ USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
SIMPLE_HISTORY_HISTORY_ID_USE_UUID = True
|
||||
|
||||
|
||||
from app.settingsLocal import *
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.postgres.expressions import ArraySubquery
|
||||
from django.db import models
|
||||
from django.db.models import OuterRef
|
||||
from django.db.models.fields.related import RelatedField
|
||||
from django.db.models.functions import JSONObject
|
||||
|
||||
|
||||
from app.utils.helpers import recursive_getattr
|
||||
@@ -78,7 +81,23 @@ class BaseQuerySet(models.QuerySet):
|
||||
for field_name, _ in self.headers().items():
|
||||
fields.append(field_name)
|
||||
|
||||
return self.values(*fields)
|
||||
if hasattr(self.model, "history"):
|
||||
qs = self.annotate(
|
||||
history=ArraySubquery(
|
||||
self.model.history.model.objects.filter(id=OuterRef("id")).values(
|
||||
json=JSONObject(
|
||||
id="history_id",
|
||||
date="history_date",
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
fields.append("history")
|
||||
|
||||
else:
|
||||
qs = self
|
||||
|
||||
return qs.values(*fields)
|
||||
|
||||
|
||||
class BaseManager(models.Manager.from_queryset(BaseQuerySet)):
|
||||
|
||||
Reference in New Issue
Block a user