Skip to content

Commit

Permalink
Added history tracking to ManyToManyFields
Browse files Browse the repository at this point in the history
...that exist on models that already have history tracking.

Note that the many-to-many selection for these fields *are* correctly saved, they're just currently not correctly displayed in the objects' history
page in Django admin (the *current* M2M selection is always shown); see jazzband/django-simple-history#1063.
  • Loading branch information
ddabble committed Mar 4, 2023
1 parent 8e85f56 commit 79bd0d7
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Generated by Django 4.1.3 on 2022-11-21 03:42

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


class Migration(migrations.Migration):

dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
("contentbox", "0014_history_date_db_index"),
]

operations = [
migrations.CreateModel(
name="HistoricalContentBox_extra_change_permissions",
fields=[
(
"id",
models.BigIntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
("m2m_history_id", models.AutoField(primary_key=True, serialize=False)),
(
"contentbox",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="contentbox.contentbox",
),
),
(
"history",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
to="contentbox.historicalcontentbox",
),
),
(
"permission",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="auth.permission",
),
),
],
options={
"verbose_name": "HistoricalContentBox_extra_change_permissions",
},
),
]
2 changes: 1 addition & 1 deletion contentbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ContentBox(models.Model):
)
last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified"))

history = HistoricalRecords(excluded_fields=['last_modified'])
history = HistoricalRecords(m2m_fields=[extra_change_permissions], excluded_fields=['last_modified'])

class Meta:
permissions = (
Expand Down
61 changes: 61 additions & 0 deletions faq/migrations/0006_historicalquestion_categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.1.3 on 2022-11-21 03:42

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


class Migration(migrations.Migration):

dependencies = [
("faq", "0005_history_date_db_index"),
]

operations = [
migrations.CreateModel(
name="HistoricalQuestion_categories",
fields=[
(
"id",
models.BigIntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
("m2m_history_id", models.AutoField(primary_key=True, serialize=False)),
(
"category",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="faq.category",
),
),
(
"history",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
to="faq.historicalquestion",
),
),
(
"question",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="faq.question",
),
),
],
options={
"verbose_name": "HistoricalQuestion_categories",
},
),
]
2 changes: 1 addition & 1 deletion faq/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Question(models.Model):
)
last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified"))

history = HistoricalRecords(excluded_fields=['last_modified'])
history = HistoricalRecords(m2m_fields=[categories], excluded_fields=['last_modified'])

class Meta:
verbose_name = _("question")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Generated by Django 4.1.3 on 2022-11-21 03:42

from django.db import migrations, models
import django.db.models.deletion
import simple_history.models


class Migration(migrations.Migration):

dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
("groups", "0012_history_date_db_index"),
]

operations = [
migrations.CreateModel(
name="HistoricalInheritanceGroup_own_permissions",
fields=[
(
"id",
models.BigIntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
("m2m_history_id", models.AutoField(primary_key=True, serialize=False)),
(
"history",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
to="groups.historicalinheritancegroup",
),
),
(
"inheritancegroup",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="groups.inheritancegroup",
),
),
(
"permission",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="auth.permission",
),
),
],
options={
"verbose_name": "HistoricalInheritanceGroup_own_permissions",
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]
3 changes: 2 additions & 1 deletion groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class InheritanceGroup(Group):
)
last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified"))

history = HistoricalRecords(excluded_fields=['last_modified'])
# TODO: Add `parents` to `m2m_fields` when https://github.com/jazzband/django-simple-history/issues/1126 is resolved
history = HistoricalRecords(m2m_fields=[own_permissions], excluded_fields=['last_modified'])

@property
def inherited_permissions(self):
Expand Down

0 comments on commit 79bd0d7

Please sign in to comment.