Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

django - Get distinct values of Queryset by field

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)

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_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?

# Hope it's something like this...
values.distinct().count()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...