Skip to content

Commit

Permalink
🐛(dashboard) adjust manager order in Consent model
Browse files Browse the repository at this point in the history
Reordered `objects` and `active_objects` in the Consent model to ensure compatibility with Django admin expectations.
Introduce a test to ensure the default Django manager is used in the admin setup, avoiding issues with custom managers.
  • Loading branch information
ssorin committed Dec 19, 2024
1 parent 759a639 commit 13ab6b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/dashboard/apps/consent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Consent(DashboardBase):
end = models.DateTimeField(_("end date"))
revoked_at = models.DateTimeField(_("revoked at"), null=True, blank=True)

active_objects = ConsentManager()
# models.Manager() must be in first place to ensure django admin expectations.
objects = models.Manager()
active_objects = ConsentManager()

class Meta: # noqa: D106
ordering = ["delivery_point"]
Expand Down
31 changes: 31 additions & 0 deletions src/dashboard/apps/consent/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Dashboard consent admin tests."""

import pytest
from django.contrib.admin.sites import AdminSite
from django.urls import reverse

from apps.consent.admin import ConsentAdmin
from apps.consent.models import Consent


@pytest.mark.django_db
def test_admin_manager_name(rf):
"""Tests the name of the manager used by the Django admin to retrieve objects.
For the Django admin, the manager used must be the default django manager
(`objects`) and not a custom manager, which could lead to an unwanted loss of
display of data.
"""
# Initialize admin
site = AdminSite()
admin = ConsentAdmin(Consent, site)

# Request admin consent
request = rf.get(reverse("admin:qcd_consent_consent_changelist"))

# Get the queryset used by admin
queryset = admin.get_queryset(request)

# The manager name must be the default Django manager: `objects`.
manager_name = queryset.model._default_manager.name
assert manager_name == "objects"

0 comments on commit 13ab6b6

Please sign in to comment.