根據DAY17的構想,我們現在來依樣畫葫蘆,把想像中的資料庫寫出來
以下的程式都寫在models.py
中,
IntegerField
: 整數類型欄位,用於儲存整數資料CharField
: 代表一個字元類型欄位,適合儲存短文字。需要指定最大字元長度 (max_length)。TextField
: 用於儲存長篇文字,比 CharField 能儲存更多文字內容,無需設定 max_length。DateTimeField
: 用於儲存日期和時間的欄位。ForeignKey
: 表示與其他模型的關聯。使用Django內置的使用者模組
models.py
from django.contrib.auth.models import User
models.py
class Task(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
due_date = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
priority = models.IntegerField()
status = models.CharField(max_length=50)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
models.py
class SharedTask(models.Model):
id = models.IntegerField(primary_key=True)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"{self.task.title} shared with {self.user.username}"
models.py
class Comment(models.Model):
id = models.IntegerField(primary_key=True)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Comment by {self.user.username} on {self.task.title}"
models.py
class ActivityLog(models.Model):
id = models.IntegerField(primary_key=True)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
action = models.CharField(max_length=50)
action_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.action} by {self.user.username} on {self.task.title}"