Created
September 2, 2015 17:40
-
-
Save p-lewis/382e554c77079424f813 to your computer and use it in GitHub Desktop.
Sample Django model of an expiring message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
from django.conf import settings | |
from django.db import models | |
from django.utils import timezone | |
class ActiveMessagesManager(models.Manager): | |
use_for_related_fields = True | |
def get_queryset(self): | |
return super(ActiveMessagesManager, self).get_queryset().filter(expiration_dt__gt=timezone.now()) | |
def default_expiration_dt(): | |
return timezone.now() + timedelta(hours=3) | |
class Message(models.Model): | |
sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='messages_sent') | |
receiver = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='messages_received') | |
text = models.TextField() | |
expiration_dt = models.DateTimeField(default=default_expiration_dt) | |
active = ActiveMessagesManager() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment