query models

9 views
Skip to first unread message

whistler whitherfaith

unread,
Oct 8, 2018, 11:19:52 AM10/8/18
to Django users
context: i'm working on an app that monitors when an employee has read a memo, how many employees have not read a memo, and how many employees have read a memo but after the deadline. when a memo is released, an employee has 30 days to read it.
              Each employee has an account. When they login; they are able to see how many memo's they haven't read and allows them to read it. once they've read it, this updates the database and an administrator is able to track who has or hasn't read a memo.
   
              so far i've come up with the following models, but i have no idea what relationships each model should have with other and what queries to perform to extract the data stated above. if there is a need to change a class or add other attributes, suggestions would be 
              appreciated. thanks.


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#app/models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta
from multiselectfield import MultiSelectField

def usr_dir_path(instance, filename):
    return 'notifications/{0}/{1}'.format(instance.airline, filename)

class Memo(models.Model):
    COMPANY_CHOICES = (
        ('WA', 'company_1'),
        ('BA', 'company_2'),
        ('CA', 'company_3'),
    )
    title = models.CharField(max_length=100)
    memo_file = models.FileField(upload_to=usr_dir_path)
    preview = models.TextField()     # 3 or 4 sentences that describe what the memo is about
    company = models.CharField(max_length=2, choices=COMPANY_CHOICES)     # name of the company that sent the memo
    date_posted = models.DateTimeField(default=timezone.now)
    date_expires = models.DateTimeField(default=timezone.now()+timedelta(days=30))

    def __str__(self):
        return self.title

    def snippet(self):
        return self.preview[:200] + '...'

class Profile(models.Model):
COMPANY_CHOICES = (
        ('WA', 'company_1'),
        ('BA', 'company_2'),
        ('CA', 'company_3'),
    )
    employee = models.ForeignKey(User, on_delete=models.CASCADE)
    emp_number = models.CharField(max_length=12)
    checks_in = MultiSelectField(choices=COMPANY_CHOICES) # creates 3 checkboxes that allow the user to pick which companies they provide service to

    def __str__(self):
        return '{0} {1}'.format(self.employee.first_name, self.employee.last_name)

class Log(models.Model):
    notification_name = models.ForeignKey(Notification, on_delete=models.CASCADE)
    username = models.ForeignKey(User, on_delete=models.CASCADE)
    date_read = models.DateTimeField(default=timezone.now, blank=True)
    
    def __str__(self):
        return self.username.username
Reply all
Reply to author
Forward
0 new messages