0

我在 Django 1.5 中有这些模型

class Number(models.Model):
    number = models.CharField("Patient's Number", max_length=12, unique=True)

class Appointment(models.Model):
    to_number = models.ForeignKey(Number)
    message = models.CharField("Message", max_length=160)

我希望Appointment模型保留另一个字段,允许我添加用户希望在实际约会时间之前通过电子邮件收到通知的次数(类似于我们在 Google 日历中添加多个弹出/电子邮件通知的方式)。由于我还是 Web 开发、Django 和数据库模型的新手,我很难决定如何创建和链接模型来实现这一点。我想到的一个解决方案是创建另一个模型并将其链接起来,Appointment如下所示:

class Notification(models.Model):
    appointment = models.ForeignKey(Appointment)
    time = models.TimeField("Time before appointment we must notify the user")

这甚至是明智的做法吗?如果没有,我应该怎么做?此外,为了通过管理控制台查看AppointmentNotificationNumber's 视图中,我应该声明什么内联堆栈(既然Number--> Apppointment--> Notification,当在Number's下查看时将它们链接为 Inline 的正确方法是什么?页)?我知道类似 [this has been ask before] ( Nested inlines in the Django admin? ),但自从 2010 年被问到,我很好奇是否有人找到了新的方法或者第二个答案@carruthd 在上述链接中仍然是最好的方法。谢谢你。

4

1 回答 1

0

如果您想为每个约会提供更多通知,请添加ManyToManyField指向另一个模型(Notification在本例中)。然后您就可以通过这种方式获取所有即将到来的通知: Appointment.notifications_set.filter(notify_time__gte=now()).

class Notification(models.Model):
    notify_time = models.DateTimeField("Notification datetime", db_index=True)

class Number(models.Model):
    number = models.CharField("Patient's Number", max_length=12, unique=True)

class Appointment(models.Model):
    to_number = models.ForeignKey(Number)
    message = models.CharField("Message", max_length=160)
    notifications = models.ManyToManyField(Notification, through=AppointmentNotifications)

class AppointmentNotifications(models.Model):
    notif = models.ForeignKey(Notification)
    appoint = models.ForeignKey(Appointment)

还有一个 table: AppointmentNotifications,无论如何都将由 Django 创建,但是如果您自己创建它,您可以稍后添加一些列(即notification_already_sent = models.BooleanField("User was informed"),您可能还希望将所有Notifications加入的列显示Appointment为内联:

管理员.py:

class AppointmentNotificationInline(admin.TabularInline):
    model = AppointmentNotification

class AppointmentAdmin(admin.ModelAdmin):
    inlines = [AppointmentNotificationInline]
于 2013-06-27T14:57:07.600 回答