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
e81f3604
authored
May 09, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up some of hte code in similar_objects, it's not prettier, if not shorter.
parent
36f4c9b5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
18 deletions
+17
-18
taggit/managers.py
+17
-18
No files found.
taggit/managers.py
View file @
e81f3604
...
@@ -164,40 +164,39 @@ class _TaggableManager(models.Manager):
...
@@ -164,40 +164,39 @@ class _TaggableManager(models.Manager):
@require_instance_manager
@require_instance_manager
def
similar_objects
(
self
):
def
similar_objects
(
self
):
lookup_kwargs
=
self
.
_lookup_kwargs
()
lookup_kwargs
=
self
.
_lookup_kwargs
()
lookup_keys
=
sorted
(
lookup_kwargs
)
qs
=
self
.
through
.
objects
.
values
(
*
lookup_kwargs
.
keys
())
qs
=
self
.
through
.
objects
.
values
(
*
lookup_kwargs
.
keys
())
qs
=
qs
.
annotate
(
n
=
models
.
Count
(
'pk'
))
qs
=
qs
.
annotate
(
n
=
models
.
Count
(
'pk'
))
qs
=
qs
.
exclude
(
**
lookup_kwargs
)
qs
=
qs
.
exclude
(
**
lookup_kwargs
)
qs
=
qs
.
filter
(
tag__in
=
self
.
all
())
qs
=
qs
.
filter
(
tag__in
=
self
.
all
())
qs
=
qs
.
order_by
(
'-n'
)
qs
=
qs
.
order_by
(
'-n'
)
# TODO: This all feels like a giant hack... giant.
# TODO: This all feels like a bit of a hack.
if
len
(
lookup_kwargs
)
==
1
:
items
=
{}
using_gfk
=
False
if
len
(
lookup_keys
)
==
1
:
# Can this just be select_related? I think so.
# Can we do this without a second query by using a select_related()
items
=
self
.
through
.
_meta
.
get_field_by_name
(
# somehow?
lookup_kwargs
.
keys
()[
0
]
f
=
self
.
through
.
_meta
.
get_field_by_name
(
lookup_keys
[
0
])[
0
]
)[
0
]
.
rel
.
to
.
_default_manager
.
in_bulk
(
objs
=
f
.
rel
.
to
.
_default_manager
.
filter
(
**
{
[
r
[
"content_object"
]
for
r
in
qs
]
"
%
s__in"
%
f
.
rel
.
field_name
:
[
r
[
"content_object"
]
for
r
in
qs
]
)
})
for
obj
in
objs
:
items
[(
getattr
(
obj
,
f
.
rel
.
field_name
),)]
=
obj
else
:
else
:
using_gfk
=
True
preload
=
defaultdict
(
set
)
preload
=
defaultdict
(
set
)
for
result
in
qs
:
for
result
in
qs
:
preload
[
result
[
"content_type"
]]
.
add
(
result
[
"object_id"
])
preload
[
result
[
"content_type"
]]
.
add
(
result
[
"object_id"
])
items
=
{}
for
ct
,
obj_ids
in
preload
.
iteritems
():
for
ct
,
obj_ids
in
preload
.
iteritems
():
ct
=
ContentType
.
objects
.
get_for_id
(
ct
)
ct
=
ContentType
.
objects
.
get_for_id
(
ct
)
items
[
ct
.
pk
]
=
ct
.
model_class
()
.
_default_manager
.
in_bulk
(
obj_ids
)
for
obj
in
ct
.
model_class
()
.
_default_manager
.
filter
(
pk__in
=
obj_ids
):
items
[(
ct
.
pk
,
obj
.
pk
)]
=
obj
results
=
[]
results
=
[]
for
result
in
qs
:
for
result
in
qs
:
# TODO: Consolidate this into dicts keyed by a tuple of the
obj
=
items
[
# (content_type, object_id) instead of the nesting.
tuple
(
result
[
k
]
for
k
in
lookup_keys
)
if
using_gfk
:
]
obj
=
items
[
result
[
"content_type"
]][
result
[
"object_id"
]]
else
:
obj
=
items
[
result
[
"content_object"
]]
obj
.
similar_tags
=
result
[
"n"
]
obj
.
similar_tags
=
result
[
"n"
]
results
.
append
(
obj
)
results
.
append
(
obj
)
return
results
return
results
...
...
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