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
aa961dd9
authored
Jan 12, 2010
by
Rob Hudson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added manager method `similar_objects`.
This finds similar objects that have the most tags in common.
parent
805389ac
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
1 deletions
+26
-1
taggit/managers.py
+9
-0
taggit/tests/__init__.py
+1
-1
taggit/tests/tests.py
+16
-0
No files found.
taggit/managers.py
View file @
aa961dd9
...
@@ -136,3 +136,12 @@ class _TaggableManager(models.Manager):
...
@@ -136,3 +136,12 @@ class _TaggableManager(models.Manager):
return
self
.
get_query_set
()
.
annotate
(
return
self
.
get_query_set
()
.
annotate
(
num_times
=
models
.
Count
(
'items'
)
num_times
=
models
.
Count
(
'items'
)
)
.
order_by
(
'-num_times'
)
)
.
order_by
(
'-num_times'
)
@require_instance_manager
def
similar_objects
(
self
):
return
TaggedItem
.
objects
.
values
(
'object_id'
,
'content_type'
)
\
.
annotate
(
models
.
Count
(
'pk'
))
\
.
exclude
(
object_id
=
self
.
object_id
)
\
.
filter
(
tag__in
=
self
.
all
())
\
.
order_by
(
'-pk__count'
)
taggit/tests/__init__.py
View file @
aa961dd9
from
taggit.tests.tests
import
AddTagTestCase
,
LookupByTagTestCase
,
TaggableFormTestCase
from
taggit.tests.tests
import
AddTagTestCase
,
LookupByTagTestCase
,
TaggableFormTestCase
,
SimilarityByTagTestCase
taggit/tests/tests.py
View file @
aa961dd9
...
@@ -79,3 +79,19 @@ class TaggableFormTestCase(BaseTaggingTest):
...
@@ -79,3 +79,19 @@ class TaggableFormTestCase(BaseTaggingTest):
apple
=
Food
.
objects
.
get
(
name
=
'apple'
)
apple
=
Food
.
objects
.
get
(
name
=
'apple'
)
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
'green'
,
'red'
,
'yummy'
,
'delicious'
])
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
'green'
,
'red'
,
'yummy'
,
'delicious'
])
self
.
assertEqual
(
Food
.
objects
.
count
(),
1
)
self
.
assertEqual
(
Food
.
objects
.
count
(),
1
)
class
SimilarityByTagTestCase
(
BaseTaggingTest
):
def
test_similarity_by_tag
(
self
):
"""Test that pears are more similar to apples than watermelons"""
apple
=
Food
.
objects
.
create
(
name
=
"apple"
)
apple
.
tags
.
add
(
"green"
,
"juicy"
,
"small"
,
"sour"
)
pear
=
Food
.
objects
.
create
(
name
=
"pear"
)
pear
.
tags
.
add
(
"green"
,
"juicy"
,
"small"
,
"sweet"
)
watermelon
=
Food
.
objects
.
create
(
name
=
"watermelon"
)
watermelon
.
tags
.
add
(
"green"
,
"juicy"
,
"large"
,
"sweet"
)
self
.
assertEqual
(
apple
.
tags
.
similar_objects
(),
[{
'pk__count'
:
3
,
'content_type'
:
13
,
'object_id'
:
6
},
{
'pk__count'
:
2
,
'content_type'
:
13
,
'object_id'
:
7
}])
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