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
e364191b
authored
Sep 25, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize TaggableManager.add, it should do fewer queries now.
parent
9229a50a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
3 deletions
+44
-3
taggit/managers.py
+15
-3
taggit/tests/tests.py
+29
-0
No files found.
taggit/managers.py
View file @
e364191b
...
...
@@ -126,9 +126,21 @@ class _TaggableManager(models.Manager):
@require_instance_manager
def
add
(
self
,
*
tags
):
for
tag
in
tags
:
if
not
isinstance
(
tag
,
self
.
through
.
tag_model
()):
tag
,
_
=
self
.
through
.
tag_model
()
.
objects
.
get_or_create
(
name
=
tag
)
str_tags
=
set
([
t
for
t
in
tags
if
not
isinstance
(
t
,
self
.
through
.
tag_model
())
])
tag_objs
=
set
(
tags
)
-
str_tags
existing
=
self
.
through
.
tag_model
()
.
objects
.
filter
(
name__in
=
str_tags
)
tag_objs
.
update
(
existing
)
for
new_tag
in
str_tags
-
set
(
t
.
name
for
t
in
existing
):
tag_objs
.
add
(
self
.
through
.
tag_model
()
.
objects
.
create
(
name
=
new_tag
))
for
tag
in
tag_objs
:
self
.
through
.
objects
.
get_or_create
(
tag
=
tag
,
**
self
.
_lookup_kwargs
())
@require_instance_manager
...
...
taggit/tests/tests.py
View file @
e364191b
from
unittest
import
TestCase
as
UnitTestCase
from
django.conf
import
settings
from
django.db
import
connection
from
django.test
import
TestCase
,
TransactionTestCase
from
taggit.models
import
Tag
,
TaggedItem
...
...
@@ -20,6 +22,19 @@ class BaseTaggingTest(object):
tags
.
sort
()
self
.
assertEqual
(
got
,
tags
)
def
assert_num_queries
(
self
,
n
,
f
,
*
args
,
**
kwargs
):
original_DEBUG
=
settings
.
DEBUG
settings
.
DEBUG
=
True
current
=
len
(
connection
.
queries
)
try
:
f
(
*
args
,
**
kwargs
)
self
.
assertEqual
(
len
(
connection
.
queries
)
-
current
,
n
,
)
finally
:
settings
.
DEBUG
=
original_DEBUG
class
BaseTaggingTestCase
(
TestCase
,
BaseTaggingTest
):
pass
...
...
@@ -97,6 +112,20 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
apple
.
delete
()
self
.
assert_tags_equal
(
self
.
food_model
.
tags
.
all
(),
[
"green"
])
def
test_add_queries
(
self
):
apple
=
self
.
food_model
.
objects
.
create
(
name
=
"apple"
)
# 1 query to see which tags exist
# + 3 queries to create the tags.
# + 6 queries to create the intermediary things (including SELECTs, to
# make sure we don't double create.
self
.
assert_num_queries
(
10
,
apple
.
tags
.
add
,
"red"
,
"delicious"
,
"green"
)
pear
=
self
.
food_model
.
objects
.
create
(
name
=
"pear"
)
# 1 query to see which tags exist
# + 4 queries to create the intermeidary things (including SELECTs, to
# make sure we dont't double create.
self
.
assert_num_queries
(
5
,
pear
.
tags
.
add
,
"green"
,
"delicious"
)
def
test_require_pk
(
self
):
food_instance
=
self
.
food_model
()
self
.
assertRaises
(
ValueError
,
lambda
:
food_instance
.
tags
.
all
())
...
...
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