From 4d1e9d247bdf3e158a65d4681ad397237bf6f754 Mon Sep 17 00:00:00 2001 From: Kenan7 Date: Sat, 6 Feb 2021 13:39:44 +0400 Subject: [PATCH] add common model so we can inherit timestamped model wherever we want, DRY --- my_awesome_project/common.py | 9 +++++++++ my_awesome_project/fileapp/models.py | 6 +++--- my_awesome_project/fileapp/tasks.py | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 my_awesome_project/common.py create mode 100644 my_awesome_project/fileapp/tasks.py diff --git a/my_awesome_project/common.py b/my_awesome_project/common.py new file mode 100644 index 0000000..976dca3 --- /dev/null +++ b/my_awesome_project/common.py @@ -0,0 +1,9 @@ +from django.db import models + + +class TimeStampedModel(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + modified_at = models.DateTimeField(auto_now=True) + + class Meta: + abstract = True diff --git a/my_awesome_project/fileapp/models.py b/my_awesome_project/fileapp/models.py index 43d7c63..3bbcfd5 100644 --- a/my_awesome_project/fileapp/models.py +++ b/my_awesome_project/fileapp/models.py @@ -1,6 +1,4 @@ -from django.conf import settings from django.contrib.auth import get_user_model -from django.db import models from django.db.models import ( CASCADE, CharField, @@ -11,6 +9,8 @@ Model, ) +from my_awesome_project.common import TimeStampedModel + User = get_user_model() # User = settings.AUTH_USER_MODEL @@ -24,7 +24,7 @@ def last_messages(self): return self.all().order_by("-id")[:10] -class FileModel(Model): +class FileModel(TimeStampedModel): name = CharField(max_length=20) description = CharField( max_length=1000, default="default file description" diff --git a/my_awesome_project/fileapp/tasks.py b/my_awesome_project/fileapp/tasks.py new file mode 100644 index 0000000..530a31c --- /dev/null +++ b/my_awesome_project/fileapp/tasks.py @@ -0,0 +1,13 @@ +from django.contrib.auth import get_user_model + +from config import celery_app + +from .models import FileModel + +User = get_user_model() + + +@celery_app.task() +def get_users_count(): + """A pointless Celery task to demonstrate usage.""" + return User.objects.count()