Giter Site home page Giter Site logo

glemmapaul / django-taggit-serializer Goto Github PK

View Code? Open in Web Editor NEW
164.0 9.0 46.0 48 KB

The Django Taggit Serializer Created for the Django REST Framework

License: BSD 3-Clause "New" or "Revised" License

Makefile 11.83% Python 88.17%
django-taggit-serializer python django-taggit

django-taggit-serializer's People

Contributors

andrlik avatar esperyong avatar glemmapaul avatar joeweiss avatar oliver-zhou avatar prokaktus avatar thenewguy avatar tpeaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-taggit-serializer's Issues

Getting KeyError on partial update (PATCH) when 'tags' not included

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.

PyPI only has older versions

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!

some bugs about

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.

Support for adding tags through multi-part forms

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...

All tags serializer

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',)

'Invalid json list. A tag list submitted in string form must be valid json.

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})

Not work with django-taggit==2.0.0

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

tags display a string in swagger but in fact a list

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!

'list' object has no attribute 'set'

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.

Sorting options

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

Missing Django Rest Framework in requirements-test.txt

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

AssertionError: `child` has not been instantiated.

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,

Moving repository to jazzband

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?

Release 0.1.6

  • Create tests for partially updating data with the serializer #9
  • Add better docs about input request #18
  • Add contributors to the package #19
  • Add comments to code #21
  • Allow multiple input formats #25
  • Allow multiple output formats #29

Saving tags give me an expected response, but nothing is actually saved into the database

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>]>

Release 0.2

Changes to be made:

  • Support multiple input formats #25
  • Support multiple output format #29
  • Add documentation for new parsers, renderers idea.
  • Push to PyPi with new tagged 0.2 version.

Add an option to serialize as an Object

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()]

pypi version is out of date

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!

Not working with Django==1.8.2 and djangorestframework==3.1.3

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)

Missing six package

It seems to me that the requirement six is missing in the setup.py. Would be great if you could fix it. Thanks!

adding example usage (REST payloads) to the readme would be very helpful

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!

RecursionError: maximum recursion depth exceeded while calling a Python object with DRF + Camel Case Renderer

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

Allow multiple input formats?

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",
...
}

'list' object has no attribute 'set'

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

AssertionError: `child` is a required argument.

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,

key error using keywords = TagListSerializerField(required=False)

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.