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
bb3dc0a9
authored
Jun 10, 2014
by
Daniel Hahler
Committed by
Florian Apolloner
Aug 14, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise ValueError with invalid types in `add`
parent
b953b74d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
6 deletions
+19
-6
taggit/managers.py
+11
-6
tests/tests.py
+8
-0
No files found.
taggit/managers.py
View file @
bb3dc0a9
...
@@ -133,12 +133,17 @@ class _TaggableManager(models.Manager):
...
@@ -133,12 +133,17 @@ class _TaggableManager(models.Manager):
@require_instance_manager
@require_instance_manager
def
add
(
self
,
*
tags
):
def
add
(
self
,
*
tags
):
str_tags
=
set
([
str_tags
=
set
()
t
tag_objs
=
set
()
for
t
in
tags
for
t
in
tags
:
if
not
isinstance
(
t
,
self
.
through
.
tag_model
())
if
isinstance
(
t
,
self
.
through
.
tag_model
()):
])
tag_objs
.
add
(
t
)
tag_objs
=
set
(
tags
)
-
str_tags
elif
isinstance
(
t
,
six
.
string_types
):
str_tags
.
add
(
t
)
else
:
raise
ValueError
(
"Cannot add {0} ({1}). Expected {2} or str."
.
format
(
t
,
type
(
t
),
type
(
self
.
through
.
tag_model
())))
# If str_tags has 0 elements Django actually optimizes that to not do a
# If str_tags has 0 elements Django actually optimizes that to not do a
# query. Malcolm is very smart.
# query. Malcolm is very smart.
existing
=
self
.
through
.
tag_model
()
.
objects
.
filter
(
existing
=
self
.
through
.
tag_model
()
.
objects
.
filter
(
...
...
tests/tests.py
View file @
bb3dc0a9
...
@@ -86,6 +86,14 @@ class TagModelTestCase(BaseTaggingTransactionTestCase):
...
@@ -86,6 +86,14 @@ class TagModelTestCase(BaseTaggingTransactionTestCase):
"category-awesome-1"
"category-awesome-1"
],
attr
=
"slug"
)
],
attr
=
"slug"
)
def
test_integers
(
self
):
"""Adding an integer as a tag should raise a ValueError (#237)."""
apple
=
self
.
food_model
.
objects
.
create
(
name
=
"apple"
)
with
self
.
assertRaisesRegexp
(
ValueError
,
(
r"Cannot add 1 \(<(type|class) 'int'>\). "
r"Expected <class 'django.db.models.base.ModelBase'> or str."
)):
apple
.
tags
.
add
(
1
)
class
TagModelDirectTestCase
(
TagModelTestCase
):
class
TagModelDirectTestCase
(
TagModelTestCase
):
food_model
=
DirectFood
food_model
=
DirectFood
tag_model
=
Tag
tag_model
=
Tag
...
...
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