glemmapaul / django-taggit-serializer Goto Github PK
View Code? Open in Web Editor NEWThe Django Taggit Serializer Created for the Django REST Framework
License: BSD 3-Clause "New" or "Revised" License
The Django Taggit Serializer Created for the Django REST Framework
License: BSD 3-Clause "New" or "Revised" License
Partials update (via PATCH request) is throwing a KeyError in _pop_tags when the 'tags' field's data is not included in the patch request.
In our situation, we patch a lot of records, and we only patch the fields that change.
Current implementation
def _pop_tags(self, validated_data):
to_be_tagged = {}
for key in self.fields.keys():
field = self.fields[key]
if isinstance(field, TagListSerializerField):
to_be_tagged[key] = validated_data.pop(key)
return (to_be_tagged, validated_data)
We need the check the existence of the key before pop'ing it, correct?
def _pop_tags(self, validated_data):
to_be_tagged = {}
for key in self.fields.keys():
field = self.fields[key]
if isinstance(field, TagListSerializerField):
if key in validated_data:
to_be_tagged[key] = validated_data.pop(key)
return (to_be_tagged, validated_data)
I'm happy to open a PR, but wanted to make sure I wasn't missing something simple or overthinking it.
Hello. This is a useful bit of code, but it seems that the most recent version isn't available via PyPI.
This seems related to #4 but doesn't seem to have been completely resolved.
If I do a pip install django-taggit-serializer
I get version 0.1.1.
And trying to install 0.1.4 explicitly fails:
$ pip install django-taggit-serializer==0.1.4
Collecting django-taggit-serializer==0.1.4
Could not find a version that satisfies the requirement django-taggit-serializer==0.1.4 (from versions: 0.1.0, 0.1.1)
No matching distribution found for django-taggit-serializer==0.1.4
If I If I go here: https://pypi.python.org/simple/django-taggit-serializer/ I only see:
Links for django-taggit-serializer
django-taggit-serializer-0.1.0.tar.gz
django-taggit-serializer-0.1.1.tar.gz
Would it be possible to push the latest version to PyPI?
Thanks!
new version have some bugs: 'Invalid json list. A tag list submitted in string'
' form must be valid json'
older version is ok .
i copy my before app project (inlcude dango-taggit-serialiezer) replace this package ,suddenly .is okay.
Using Django and Django Rest Framework connected to an Angular app, I'm trying to add tags to every WorkSample model I create. I create new instances of this model by submitting POST data to API endpoints. In this case, the model and form include a user-submitted image file, so I have to convert the POST data to a FormData object before POSTing so that DRF can interpret the image file (making sure to include parser_classes = ( FormParser, MultiPartParser,)
in the model serializer to interpret a multi-part form).
That was working great before I added the tags field, using django-taggit and django-taggit-serializer. But trying to submit tag values through the FormData object results in this error: "{tags: ["Invalid json list. A tag list submitted in string form must be valid json."]}"
Is support for multi-part form submission an option? Wondering what I could do differently...
Is there a serializer for the tags itself, not a tag field ?
Currently I have:
from taggit.models import Tag
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = ('name',)
When I try to create object from test client I get this error {'tag': ['Invalid json list. A tag list submitted in string form must be valid json.']}
. Here is the code:
self.client.post('/api/notes/', data={'tag': ["xyz"], 'iv': 'random', 'content': "New content",
'date': self.current_date_time})
Related to #7
django-taggit==2.0.0
django-taggit-serializer==0.1.7
class Blabla(TaggitSerializer, serializers.ModelSerializer):
tags = TagListSerializerField()
...
...site-packages/taggit_serializer/serializers.py", line 108, in _save_tags
getattr(tag_object, key).set(*tag_values)
TypeError: set() takes 2 positional arguments but 3 were given
All good with django-taggit==1.5.1
This is related to issue #16
Since there has been no activity since 2018, I have created a new fork where I'll be maintaining this. Feel free to open new pull requests.
I use drf-yasg to create the swagger page,but it shows that tags filed is a string.
And I tried to make a new filed by:
class MyTagListSerializerField(TagListSerializerField, serializers.ListField):
pass
and it worked. So i think is the TagListSerializerField should Inherit from serializers.ListField but not serializers.Field will be better.
Sure, it's just an immature idea. Waitting for your help!
This code getattr(tag_object, key) inside "_save_tags" method is returning a list instead of a TaggableManager class.
def _save_tags(self, tag_object, tags):
for key in tags.keys():
tag_values = tags.get(key)
getattr(tag_object, key).set(*tag_values)
return tag_object
It happens during update object with tags.
I'd like an order_by
property on the serializer so I can specify a custom order and not have to sort on the front end. #22
Doing a standalone checkout and virtualenv creation, and then doing a python runtests.py will fail due to lack of rest_framework.
ImportError: No module named rest_framework
Traceback (most recent call last):
File "runtests.py", line 43, in <module>
raise ImportError("To fix this error, run: pip install -r requirements-test.txt")
ImportError: To fix this error, run: pip install -r requirements-test.txt
Paul,
With the new version 0.1.1 the assertion error persists. Then including the child:
tags = TagListSerializerField(child=models.CharField)
A new assertion error is generated.
web_1 | File "/app/contratos/api/documents/serializers.py", line 8, in <module>
web_1 | class TemplateSerializer(TaggitSerializer,serializers.ModelSerializer):
web_1 | File "/app/contratos/api/documents/serializers.py", line 10, in TemplateSerializer
web_1 | tags = TagListSerializerField(child=models.CharField)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1127, in __init__
web_1 | assert not inspect.isclass(self.child), '`child` has not been instantiated.'
web_1 | AssertionError: `child` has not been instantiated.
regards,
Hey there @glemmaPaul. I've been watching this repo for quiet some time now and it seems that lately very few activity has been put into it, which is understandable. Therefore I want to suggest to move this repository to Jazzband, so a much bigger community of maintainers could take on it. How does that sound to you?
Right now we don't have any, it needs to be properly documented.
Using Django==1.11.9, django-taggit==0.22.2, and django-taggit-serializer==0.1.5
When doing a 'PATCH' to a model with tags, the response reflects the new tags, but on a subsequent 'GET', the tags do not seem to have been updated.
tags": [
"test tag 1",
"test tag 2",
],
Manually doing an add() works fine via the shell
In [6]: model_with_tags.tags.add('test')
In [7]: model_with_tags.tags.all()
Out[7]: <QuerySet [<Tag: asdf>, <Tag: asdfzxcv>, <Tag: test>]>
Do to some depreciated methods this no longer works, is someone actively maintaining this or should I create a fork?
How to make taggit tags api endpoints with taggit-serializer
Hi!
For fix suggestion, see #31
I think, that adding test matrix make tests more explicit and looks nicely!
Currently build failed due travis git checkout
error. I'll mention when it'll pass.
upd. @glemmaPaul it's works now!
https://travis-ci.org/glemmaPaul/django-taggit-serializer/builds/133786239
Currently the serializer outputs something like this:
"tags": [
"pixel art",
"unity"
]
iIt would be nice to have the option for it to be:
{
"tags":[
{
"id":2,
"name":"web"
},
{
"id":1,
"name":"django"
}
]
}
Currently I manged to do it changing on serializers:
value = [{ 'id':tag.id, 'name': tag.name } for tag in value.all()]
Hi, great little package, very useful. The version on pypi claims to be 0.1.1 but is missing at least from 5c62cb0 onwards. Could you update pypi and/or bump the version number?
I needed to add the child
param to my field which shouldn't be required as far as I can tell.
tags = TagListSerializerField(child=serializers.CharField())
Thanks!
This isn't working for me. I added the field to my serializer as shown in the readme. With it I cannot add tags via the api. Also, if I update a record with the api that already had tags, the tags are cleared.
Not sure if it is relevant, but I had to use a custom tag model per taggit docs since I use UUID primary keys.
class TaggedFoo(TaggedItemBase):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
content_object = models.ForeignKey('Foo')
class Foo(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
tags = TaggableManager(through=TaggedFoo)
It seems to me that the requirement six
is missing in the setup.py. Would be great if you could fix it. Thanks!
First and foremost: thanks for creating this, your work is appreciated!
I am unfortunately unable to get it working. I've got the GET request for my model returning tags: []
as expected, but when I call a PATCH method and submit the data as either strings, a string representation of an array, etc, it doesn't seem to work.
Because there isn't documentation on what my payload should look like, I've just tried everything I can think of, and it always comes back with an empty tag list, which makes it hard to know if I've configured something wrong or I'm sending the wrong payload. I've tried the following values for "tags" in my PATCH request:
foo
foo bar
["foo", "bar"]
["foo"]
so far, nothing gets created in the database, no error is thrown, etc, so it's unclear what I might be doing wrong.
I'd be happy to contribute back some examples if/when I get it working, but right now I'm a bit stymied!
AssertionError: (u"Creating a ModelSerializer without either the 'fields' attribute or the 'exclude' attribute has been deprecated since 3.3.0, and is now disallowed. Add an explicit fields = '__all__' to the TestModelSerializer serializer.",)
Not sure if this is on taggit-serializer's side or camel-case's side, but I'm going to report here first before I go to bed.
When using with camel-case renderer the interpreter blows up with a recursion error. I'll try to reproduce with a simplified example tomorrow, but it seems fairly straightforward to reproduce.
RecursionError: maximum recursion depth exceeded while calling a Python object
Fatal Python error: Cannot recover from stack overflow.
Thread 0x000070000f78e000 (most recent call first):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 575 in readinto
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/django/core/servers/basehttp.py", line 164 in handle
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socketserver.py", line 684 in __init__
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/django/core/servers/basehttp.py", line 99 in __init__
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socketserver.py", line 357 in finish_request
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socketserver.py", line 628 in process_request_thread
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862 in run
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914 in _bootstrap_inner
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 882 in _bootstrap
Current thread 0x000070000f28b000 (most recent call first):
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/taggit_serializer/serializers.py", line 10 in __init__
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/taggit_serializer/serializers.py", line 21 in __getitem__
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
File "/home/pnovotnak/Documents/myproject/env/lib/python3.5/site-packages/djangorestframework_camel_case/util.py", line 21 in camelize
...
cc @javierbq
Hello, The only thing I'm personally see wrong with Treebeard and Django 3.0 is that 3.0 officially deprecated ugettext_lazy, and if you run python -Wall, you'll get a ton of RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
My use case is simply to send a comma seperated list to the backend. So basically a string in my json payload.
{
...
tags: "first, second, third",
...
}
Why haven't I done that
joeiss, thenewguy, esperyong
This code getattr(tag_object, key)
inside "_save_tags" method is returning a list instead of a TaggableManager class.
I had to do it this way to get it working in my project:
def _save_tags(self, tag_object, tags):
# Had to override because tag_object.tags is returning a list
for key in tags.keys():
tag_values = tags.get(key)
Photo.objects.get(id=tag_object.id).tags.set(*tag_values)
return tag_object
Hi,
I'm getting an assertion error after including the app to my project following the instructions.
The relevant traceback is as follows:
class TemplateSerializer(TaggitSerializer,serializers.ModelSerializer):
File "/app/contratos/api/documents/serializers.py", line 8, in TemplateSerializer
tags = TagListSerializerField()
File "/usr/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1126, in __init__
assert self.child is not None, '`child` is a required argument.
AssertionError: `child` is a required argument
As reference take into account
$uname -a
Darwin Juans-MacBook-Pro.local 13.4.0 Darwin Kernel Version 13.4.0: Wed Dec 17 19:05:52 PST 2014; root:xnu-2422.115.10~1/RELEASE_X86_64 x86_64
requirements:
Django==1.7.6
django-taggit==0.12.3
django-taggit-serializer==0.1.0
djangorestframework==3.0.3
Everything is running inside boot2docker postgres 9.4
regards,
Dear All,
I got a KeyError using
# models
keywords = TaggableManager(_('Keywords'),
blank=True,
help_text=_(''))
# serializer
class MySerializer(TaggitSerializer, serializers.ModelSerializer):
***
keywords = TagListSerializerField(required=False)
Django 1.8.1, DRF 3.1.1, taggit 0.14.0
please as soon as soon as possible
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.