通过字段获得不同的Queryset值。

时间:2021-09-13 01:38:21

I've got this model:

我有这模型:

class Visit(models.Model):
    timestamp  = models.DateTimeField(editable=False)
    ip_address = models.IPAddressField(editable=False)

If a user visits multiple times in one day, how can I filter for unique rows based on the ip field? (I want the unique visits for today)

如果用户在一天中多次访问,如何根据ip字段对唯一行进行筛选?(我想要今天独一无二的参观)

today = datetime.datetime.today()
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)

visits = Visit.objects.filter(timestamp__range=(yesterday, today)) #.something?

EDIT:

编辑:

I see that I can use:

我看到我可以使用:

Visit.objects.filter(timestamp__range=(yesterday, today)).values('ip_address')

to get a ValuesQuerySet of just the ip fields. Now my QuerySet looks like this:

获取仅由ip字段组成的ValuesQuerySet。现在我的QuerySet看起来是这样的:

[{'ip_address': u'127.0.0.1'}, {'ip_address': u'127.0.0.1'}, {'ip_address':
 u'127.0.0.1'}, {'ip_address': u'127.0.0.1'}, {'ip_address': u'127.0.0.1'}]

How do I filter this for uniqueness without evaluating the QuerySet and taking the db hit?

如何在不计算QuerySet并对db进行命中的情况下对其进行惟一性过滤?

# Hope it's something like this...
values.distinct().count()

2 个解决方案

#1


34  

What you want is:

你想要的是:

Visit.objects.filter(stuff).values("ip_address").annotate(n=models.Count("pk"))

What this does is get all ip_addresses and then it gets the count of primary keys (aka number of rows) for each ip address.

它所做的是获取所有的ip_address,然后获取每个ip地址的主键数(即行数)。

#2


14  

With Alex Answer I also have the n:1 for each item. Even with a distinct() clause.

对于Alex的回答,我也有n:1的每一项。即使有不同的()条款。

It's weird because this is returning the good numbers of items :

这很奇怪,因为它返回的是好数量的物品:

Visit.objects.filter(stuff).values("ip_address").distinct().count()

But when I iterate over "Visit.objects.filter(stuff).values("ip_address").distinct()" I got much more items and some duplicates...

但是当我遍历“Visit.objects.filter(东西).values(“ip_address”).distinct()”时我有更多的东西和一些复制品……

EDIT :

编辑:

The filter clause was causing me troubles. I was filtering with another table field and a SQL JOIN was made that was breaking the distinct stuff. I used this hint to see the query that was really used :

过滤条款给我带来了麻烦。我使用另一个表字段进行过滤,并创建了一个SQL JOIN,它破坏了不同的内容。我使用这个提示来查看真正使用的查询:

q=Visit.objects.filter(myothertable__field=x).values("ip_address").distinct().count()
print q.query

I then reverted the class on witch I was making the query and the filter to have a join that doesn't rely on any "Visit" id.

然后,我将正在进行查询的witch的类还原为一个不依赖任何“Visit”id的连接。

hope this helps

希望这有助于

#1


34  

What you want is:

你想要的是:

Visit.objects.filter(stuff).values("ip_address").annotate(n=models.Count("pk"))

What this does is get all ip_addresses and then it gets the count of primary keys (aka number of rows) for each ip address.

它所做的是获取所有的ip_address,然后获取每个ip地址的主键数(即行数)。

#2


14  

With Alex Answer I also have the n:1 for each item. Even with a distinct() clause.

对于Alex的回答,我也有n:1的每一项。即使有不同的()条款。

It's weird because this is returning the good numbers of items :

这很奇怪,因为它返回的是好数量的物品:

Visit.objects.filter(stuff).values("ip_address").distinct().count()

But when I iterate over "Visit.objects.filter(stuff).values("ip_address").distinct()" I got much more items and some duplicates...

但是当我遍历“Visit.objects.filter(东西).values(“ip_address”).distinct()”时我有更多的东西和一些复制品……

EDIT :

编辑:

The filter clause was causing me troubles. I was filtering with another table field and a SQL JOIN was made that was breaking the distinct stuff. I used this hint to see the query that was really used :

过滤条款给我带来了麻烦。我使用另一个表字段进行过滤,并创建了一个SQL JOIN,它破坏了不同的内容。我使用这个提示来查看真正使用的查询:

q=Visit.objects.filter(myothertable__field=x).values("ip_address").distinct().count()
print q.query

I then reverted the class on witch I was making the query and the filter to have a join that doesn't rely on any "Visit" id.

然后,我将正在进行查询的witch的类还原为一个不依赖任何“Visit”id的连接。

hope this helps

希望这有助于