zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from rest_framework import serializers
import json
 
class TagsField(serializers.JSONField):
    def to_representation(self, tags):
        return [t for t in tags.split(" ") if t != ""]
 
    def to_internal_value(self, tags):
        return " ".join([t.strip() for t in tags])
 
def parse_tags_input(tags):
    if tags is None:
        return []
    
    if isinstance(tags, str):
        try:
            r = json.loads(tags)
            if isinstance(r, list):
                return r
            else:
                raise Exception("Invalid tags string")
        except:
            return []
    elif isinstance(tags, list):
        return list(map(str, tags))
    else:
        return []