Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django筛选器查询(按类型)_Python_Mysql_Django_Django Views_Django Queryset - Fatal编程技术网

Python Django筛选器查询(按类型)

Python Django筛选器查询(按类型),python,mysql,django,django-views,django-queryset,Python,Mysql,Django,Django Views,Django Queryset,我想显示每种通知类型的一个通知和最近的一个通知。 我的问题是如何获取每种通知类型并显示该通知类型的最新版本 notifications = Notification.objects.filter(**condition). \ exclude(notification_user__in=users). \ order_by('notificationType__priority','-start_date')[:1] models.py class Notification(m

我想显示每种通知类型的一个通知和最近的一个通知。 我的问题是如何获取每种通知类型并显示该通知类型的最新版本

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()
这仅显示与通知类型无关的最新通知。 有什么建议吗

更新

现在,我将通知插入到列表中。 但这样做是行不通的 我的想法是,如果通知类型优先级值(unique int)不存在,则添加到列表中

notifications=[]    
if n.objects.filter(notification_type__priority=notifications).exists():
    notifications.append(n)

您可以单独捕获所有通知,然后将它们合并到一个
QuerySet

c1 = Notification.objects.filter(condition=[CONDITION1_HERE]). \
    exclude(notification_user__in=users). \
    order_by('notificationType__priority','-start_date')[:1]

c2 = Notification.objects.filter(condition=[CONDITION2_HERE]). \
    exclude(notification_user__in=users). \
    order_by('notificationType__priority','-start_date')[:1]

notifications = c1 | c2  # A QuerySet that merges c1 and c2

通过使用for循环,这可以变得更智能,这样您就可以使用
**条件
,而不必定义
[CONDITION1\u HERE]
[CONDITION2\u HERE]
,但我需要查看模型的外观。

从NotificationType和最新的相关通知进行查询。你可以参考上面的django文档或进一步的灵感。

不错,但有一个问题。通知类型是动态的,这意味着用户可以添加他自己的通知类型,如果我有2个以上的通知类型,那么这种方式不起作用,这就是我的想法-你可以用任何方式稍微看看模型吗?这将帮助我提供一个动态解决方案。添加了模型,已请求