Skip to content

Commit

Permalink
Add created_at timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Astori authored and Anna Astori committed Jul 20, 2024
1 parent 1c1e81e commit ade65b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ def clear(self):
using=db,
)

def most_recent(self):
return self.latest("created_at")

def least_recent(self):
return self.earliest("created_at")

def most_common(self, min_count=None, extra_filters=None):
queryset = (
self.get_queryset(extra_filters)
Expand Down
3 changes: 3 additions & 0 deletions taggit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.utils.translation import gettext
from django.utils.translation import gettext_lazy as _
from django.utils.translation import pgettext_lazy
from django.utils import timezone


try:
from unidecode import unidecode
Expand Down Expand Up @@ -183,6 +185,7 @@ class Meta:


class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
created_at = models.DateTimeField(default=timezone.now, blank=True, null=True)
class Meta:
verbose_name = _("tagged item")
verbose_name_plural = _("tagged items")
Expand Down
10 changes: 10 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,16 @@ def test_extra_fields(self):

self.assertEqual(apple, self.food_model.objects.get(tags__official=False))

def test_timestamps(self):
apple = self.food_model.objects.create(name="apple")
apple.tags.add("red")
apple.tags.add("delicious")
most_recent = apple.tags.most_recent()
least_recent = apple.tags.least_recent()

self.assertEqual(most_recent.name, "delicious")
self.assertEqual(least_recent.name, "red")

def test_get_tags_with_count(self):
apple = self.food_model.objects.create(name="apple")
apple.tags.add("red", "green", "delicious")
Expand Down

0 comments on commit ade65b2

Please sign in to comment.