Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-taggit
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d513882d
authored
Jan 13, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Preload related tags.
parent
56e1227f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
7 deletions
+26
-7
taggit/managers.py
+23
-4
taggit/tests/tests.py
+3
-3
No files found.
taggit/managers.py
View file @
d513882d
from
collections
import
defaultdict
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.models
import
ContentType
from
django.db
import
models
from
django.db
import
models
from
django.db.models.fields.related
import
ManyToManyRel
from
django.db.models.fields.related
import
ManyToManyRel
...
@@ -137,9 +139,26 @@ class _TaggableManager(models.Manager):
...
@@ -137,9 +139,26 @@ class _TaggableManager(models.Manager):
@require_instance_manager
@require_instance_manager
def
similar_objects
(
self
):
def
similar_objects
(
self
):
return
TaggedItem
.
objects
.
values
(
'object_id'
,
'content_type'
)
\
qs
=
TaggedItem
.
objects
.
values
(
'object_id'
,
'content_type'
)
\
.
annotate
(
models
.
Count
(
'pk'
))
\
.
annotate
(
n
=
models
.
Count
(
'pk'
))
\
.
exclude
(
object_id
=
self
.
object_id
)
\
.
exclude
(
object_id
=
self
.
object_id
)
\
.
filter
(
tag__in
=
self
.
all
())
\
.
filter
(
tag__in
=
self
.
all
())
\
.
order_by
(
'-pk__count'
)
.
order_by
(
'-n'
)
preload
=
defaultdict
(
set
)
for
result
in
qs
:
preload
[
result
[
"content_type"
]]
.
add
(
result
[
"object_id"
])
items
=
{}
for
ct
,
obj_ids
in
preload
.
iteritems
():
ct
=
ContentType
.
objects
.
get_for_id
(
ct
)
items
[
ct
.
pk
]
=
dict
((
o
.
pk
,
o
)
for
o
in
ct
.
model_class
()
.
_default_manager
.
filter
(
pk__in
=
obj_ids
)
)
results
=
[]
for
result
in
qs
:
obj
=
items
[
result
[
"content_type"
]][
result
[
"object_id"
]]
obj
.
similar_tags
=
result
[
"n"
]
results
.
append
(
obj
)
return
results
taggit/tests/tests.py
View file @
d513882d
...
@@ -90,6 +90,6 @@ class SimilarityByTagTestCase(BaseTaggingTest):
...
@@ -90,6 +90,6 @@ class SimilarityByTagTestCase(BaseTaggingTest):
watermelon
=
Food
.
objects
.
create
(
name
=
"watermelon"
)
watermelon
=
Food
.
objects
.
create
(
name
=
"watermelon"
)
watermelon
.
tags
.
add
(
"green"
,
"juicy"
,
"large"
,
"sweet"
)
watermelon
.
tags
.
add
(
"green"
,
"juicy"
,
"large"
,
"sweet"
)
s
elf
.
assertEqual
(
apple
.
tags
.
similar_objects
(),
s
imilar_objs
=
apple
.
tags
.
similar_objects
()
[{
'pk__count'
:
3
,
'content_type'
:
13
,
'object_id'
:
6
},
{
'pk__count'
:
2
,
'content_type'
:
13
,
'object_id'
:
7
}
])
self
.
assertEqual
(
similar_objs
,
[
pear
,
watermelon
])
self
.
assertEqual
(
map
(
lambda
x
:
x
.
similar_tags
,
similar_objs
),
[
3
,
2
])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment