Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix custom managers being ignored leading to DoesNotExist exception #318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
# created or updated?
delta = {}
if not created:
old_model = sender.objects.get(pk=instance.pk)
old_model = sender._base_manager.get(pk=instance.pk)
delta = model_delta(old_model, instance)

if not delta and getattr(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_app/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db import models


class SoftDeleteManager(models.Manager):
def get_queryset(self):
return super().get_queryset().exclude(deleted=True)
24 changes: 24 additions & 0 deletions tests/test_app/migrations/0002_custommanagermodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.11 on 2024-12-13 17:18

from django.db import migrations, models
import django.db.models.manager


class Migration(migrations.Migration):
dependencies = [
("test_app", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="CustomManagerModel",
fields=[
("id", models.BigAutoField(primary_key=True, serialize=False)),
("name", models.CharField(default="test data", max_length=50)),
("deleted", models.BooleanField(default=False)),
],
managers=[
("all_objects", django.db.models.manager.Manager()),
],
),
]
11 changes: 11 additions & 0 deletions tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.db import models

from .managers import SoftDeleteManager


class Model(models.Model):
id = models.AutoField(primary_key=True)
Expand Down Expand Up @@ -51,3 +53,12 @@ class BigIntM2MModel(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
test_m2m = models.ManyToManyField(BigIntModel)


class CustomManagerModel(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50, default="test data")
deleted = models.BooleanField(default=False)

all_objects = models.Manager()
objects = SoftDeleteManager()
20 changes: 20 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
BigIntForeignKeyModel,
BigIntM2MModel,
BigIntModel,
CustomManagerModel,
ForeignKeyModel,
M2MModel,
Model,
Expand Down Expand Up @@ -52,6 +53,7 @@ def test_no_issues(capsys: pytest.CaptureFixture):
UUIDForeignKeyModel,
UUIDM2MModel,
UUIDModel,
CustomManagerModel,
],
)
class TestDjangoCompat:
Expand All @@ -75,6 +77,10 @@ def fk_model(self):
def m2m_model(self):
return M2MModel

@pytest.fixture
def custom_manager_model(self):
return CustomManagerModel

@pytest.fixture
def _audit_logger(self, monkeypatch):
def _crud(*args, **kwargs):
Expand Down Expand Up @@ -131,6 +137,20 @@ def test_m2m_clear(self, model, m2m_model):
data = json.loads(crud_event.object_json_repr)[0]
assert [str(d) for d in data["fields"]["test_m2m"]] == []

def test_custom_manager(self, custom_manager_model):
obj = custom_manager_model.objects.create(deleted=True)
crud_event_qs = CRUDEvent.objects.filter(
object_id=obj.id, content_type=ContentType.objects.get_for_model(obj)
)
assert crud_event_qs.count() == 1

obj.name = "this should not fail"
obj.save()
crud_event_qs = CRUDEvent.objects.filter(
object_id=obj.id, content_type=ContentType.objects.get_for_model(obj)
)
assert crud_event_qs.count() == 2

@pytest.mark.usefixtures("no_changed_fields_skip")
def test_update_skip_no_changed_fields(self, model):
obj = model.objects.create()
Expand Down