django-taggit-serializer icon indicating copy to clipboard operation
django-taggit-serializer copied to clipboard

Allow multiple input formats?

Open philippeluickx opened this issue 9 years ago • 11 comments

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

philippeluickx avatar Oct 24 '15 18:10 philippeluickx

Hmmm that is more a renderer thing, Maybe we could make a parser option, like this:

field = TagListSerializerField(parser=CommaSeperatedTagsParser())

What do you think @philippeluickx ?

glemmaPaul avatar Oct 28 '15 16:10 glemmaPaul

Sounds like a good idea to me to seperate it with parsers. That will allow to extend with customized parsers if needed.

philippeluickx avatar Oct 28 '15 16:10 philippeluickx

Okay sounds good @philippeluickx, let's make this an enhancement for the next version.

glemmaPaul avatar Nov 02 '15 15:11 glemmaPaul

You're a hero!

philippeluickx avatar Nov 02 '15 15:11 philippeluickx

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 the models 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 my issue related to this one, or am I just doing something wrong?

jameskane05 avatar Dec 04 '15 00:12 jameskane05

@jameskane05 Did you found solution to this issue. I am getting this issue too.

vaibhav-jain avatar Feb 17 '16 07:02 vaibhav-jain

I believe I did but I forget my solution... Let me get back to you tonight.

On Wed, Feb 17, 2016 at 1:00 AM, vaibhav jain [email protected] wrote:

@jameskane05 https://github.com/jameskane05 Did you found solution to this issue. I am getting this issue too.

— Reply to this email directly or view it on GitHub https://github.com/glemmaPaul/django-taggit-serializer/issues/25#issuecomment-185061384 .

jameskane05 avatar Feb 17 '16 22:02 jameskane05

@jameskane05 I get it working by submitting in this format ["tag1", "tag2"] .

vaibhav-jain avatar Feb 18 '16 04:02 vaibhav-jain

@glemmaPaul

field = TagListSerializerField(parser=CommaSeperatedTagsParser())

maybe it will be better to provide custom callable? Not object instance, but callable. And then, in to_internal_value, we can call:

if self.parser:
    data = self.parser(data)

I can implement this, if you like my idea ☕

prokaktus avatar May 08 '16 18:05 prokaktus

@prokaktus Thank you very much for the offer and I'm definitely not saying no to that, just create a PR with the added changes.

I'm going to check out why the tests are failing this weekend (hopefully 📆 )!

glemmaPaul avatar May 27 '16 03:05 glemmaPaul

I came across this issue while google searching the problem when i encountered the same issue performing a POST. I eventually overcame the problem by changing the way i was making the POST.

Instead of (i am using HTTPie): http POST http://127.0.0.1:8000/api/v1/test/ test_text="Testing API with tags" tags=["test"]

(or whatever form generated the error for you) try:

http --json POST http://127.0.0.1:8000/api/v1/test/ test_text="Testing API with tags" tags='["test"]'

Hope this helps anyone else who encounters the issue.

shazshah avatar Aug 28 '17 17:08 shazshah