Skip to content

Commit

Permalink
🗃️ Make AbstractDocument.updated_at an auto_now field
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Jan 2, 2024
1 parent 0cb4adc commit 7c59d5b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/filingcabinet/migrations/0029_alter_document_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.4 on 2023-12-18 17:14

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("filingcabinet", "0028_alter_collectiondirectory_depth_and_more"),
]

operations = [
migrations.AlterField(
model_name="document",
name="updated_at",
field=models.DateTimeField(auto_now=True, null=True),
),
]
10 changes: 9 additions & 1 deletion src/filingcabinet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ class AbstractDocument(models.Model):
)

created_at = models.DateTimeField(default=timezone.now, null=True)
updated_at = models.DateTimeField(default=timezone.now, null=True)
updated_at = models.DateTimeField(
auto_now=True, null=True
) # Allow null for old values
published_at = models.DateTimeField(default=None, null=True, blank=True)

num_pages = models.PositiveIntegerField(default=0)
Expand Down Expand Up @@ -460,6 +462,12 @@ def has_format(self, format):
def has_format_webp(self):
return self.has_format("webp")

def save(self, *args, **kwargs):
if "update_fields" in kwargs:
kwargs["update_fields"] = {"last_modified_at"}.union("update_fields")

super().save(*args, **kwargs)


class Document(AbstractDocument):
class Meta(AbstractDocument.Meta):
Expand Down

0 comments on commit 7c59d5b

Please sign in to comment.