【发布时间】:2015-07-16 20:40:11
【问题描述】:
我想显示每种通知类型的一个通知和最新的通知。 我的问题是如何获取每种通知类型并显示该通知类型的最新消息。
notifications = Notification.objects.filter(**condition). \
exclude(notification_user__in=users). \
order_by('notificationType__priority','-start_date')[:1]
models.py
class Notification(models.Model):
title = models.CharField(max_length=75)
description = models.TextField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
application = models.ManyToManyField('Products.Application')
notificationType = models.ForeignKey(NotificationType)
url = models.URLField(null=True, blank=True)
region = models.ManyToManyField('Geolocations.Region', null=True, blank=True)
image = models.ImageField(null=True, blank=True)
user = models.ManyToManyField(User, through='Notification_User')
class NotificationType(models.Model):
type = models.CharField(max_length=30)
priority = models.IntegerField(max_length=3)
color = RGBColorField()
这仅显示独立于通知类型的最新通知。 有什么建议吗?
更新
现在我将通知插入到列表中。 但是这样是行不通的 我的想法是如果 notification_type 优先级值(唯一 int)不存在,添加到列表中
notifications=[]
if n.objects.filter(notification_type__priority=notifications).exists():
notifications.append(n)
【问题讨论】:
标签: python mysql django django-views django-queryset