Django模型建议链接的ForeignKeys方法 [英] Django model advice for chained ForeignKeys approach
问题描述
class Number(models.Model):
number = models。 CharField(Patient's Number,max_length = 12,unique = True)
class预约(models.Model):
to_number = models.ForeignKey(Number)
message = models .CharField(Message,max_length = 160)
我想要预约
模型以容纳另一个字段,这将允许我添加用户希望在实际约会时间之前通过电子邮件通知多少次(类似于我们在Google日历中添加多个弹出/电子邮件通知) 。因为我仍然在Web开发新手,Django和数据库模型,我有一个很难决定如何创建和连接起来的模型来实现这一目标。我想到了一个解决方案是创建另一个模型,并用约会
这样的链接起来:
类通知(models.Model):
约会= models.ForeignKey(约会)
time = models.TimeField(预约之前必须通知用户的时间)
时,即使是明智的做法?如果没有,我该怎么办?另外,为了在中查看
通过管理控制台的视图,我应该声明内联堆栈(从现在开始,预约
和通知
号 Number
- > Apppointment
- > 通知
,在编号
的页面)查看时是什么最多的内嵌链接那些正道?我知道[这已经被问过了](在Django管理员中嵌套内联),但是由于在2010年被问到,我很好奇,如果有人找到了一种新的方式来做,或者如果上述链接中的@carruthd的第二个答案仍然是最好的方法。谢谢。
如果您想要每个约会的更多通知,请添加 ManyToManyField
指向另一个模型(通知
在这种情况下)。那么你可以通过这种方式获得所有即将到来的通知:
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预约(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(通知)
约会= models.ForeignKey(约会)
还有一个表: AppointmentNotifications
,将由Django创建,但如果您自己创建,你再稍后添加一些列(即。 notification_already_sent = models.BooleanField(用户被通知)
,您也可能希望显示所有通知
预约
作为内联:
admin.py:
class AppointmentNotificationInline(admin.TabularInline):
model = AppointmentNotification
class AppointmentAdmin(admin.ModelAdmin):
inlines = [AppointmentNotificationInline ]
I have these models in 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)
I would like the Appointment
model to hold another field that will allow me to add how many times the user wants to be notified via email before the actual appointment time (similar to how we add multiple popup/email notifications in Google calendar). Since I'm still a newbie in web development, Django and database models, I have a hard time deciding how to create and link up models to achieve this. One solution that I thought of is to create another model and link it up with Appointment
like this:
class Notification(models.Model):
appointment = models.ForeignKey(Appointment)
time = models.TimeField("Time before appointment we must notify the user")
Is that even a sensible approach? If not, what should I do instead? Also, in order to see both Appointment
and Notification
in Number
's view via admin console, what should I declare the Inline stacks (since now that Number
--> Apppointment
--> Notification
, what is the right way to link those up as Inline when viewed under Number
's page)? I know something like [this has been asked before] (Nested inlines in the Django admin?), but since it's been asked in 2010, I'm curious as to if anyone has found a new way to do it or if the second answer by @carruthd in the aforementioned link is still the best way to go about it. Thank you.
If you want more notifications for each appointment, add ManyToManyField
pointing to another model (Notification
in this case). Then you're able to get all upcoming Notifications in this way:
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)
There's one more table: AppointmentNotifications
, which would be created by Django anyway, but if you create it by yourself, you're able to add some columns later (ie. notification_already_sent = models.BooleanField("User was informed")
and you also probably want to display all Notifications
joined to each Appointment
as an inline:
admin.py:
class AppointmentNotificationInline(admin.TabularInline):
model = AppointmentNotification
class AppointmentAdmin(admin.ModelAdmin):
inlines = [AppointmentNotificationInline]
这篇关于Django模型建议链接的ForeignKeys方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!