我想显示每个通知类型和最近的一个通知。 我的问题是如何获取每种通知类型并显示最新的通知类型。Django-Filter按类型查询
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()
这只能说明最近的通知独立于通知类型的。 有什么建议吗?
UPDATE
现在我插入通知到列表中。 但在这种方式行不通 我的想法是,如果NOTIFICATION_TYPE的优先级值(唯一INT)不存在,要添加到列表中
notifications=[]
if n.objects.filter(notification_type__priority=notifications).exists():
notifications.append(n)
不错,但有一个问题。通知类型是动态的,这意味着用户可以添加他自己的通知类型,如果我有2个以上的方式不工作 –
这就是我的想法 - 任何方式你可以看看模型?这将帮助我提供一个动态的解决方案。 – Hybrid
增加了模型,要求 –