Reputation: 421
I have a mysql objects.filter that I was trying to serialise to json. My fields are
domain, generated_on, id, priority_mx, record, record_points_to, ttl
However, after I serialise the data like
from getdata.models import record_search
query_data = record_search.objects.filter(**filter_kwargs).only("domain", "record", "record_points_to", "priority_mx", "ttl", "generated_on")
data = serializers.serialize(lang, query_data)
my data has one additional field
model: "getdata.record_search"
I tried to remove it, by trying to select the fields as
data = serializers.serialize(lang, query_data, fields=('domain','record_points_to'))
but model: "getdata.record_search"
still remains in my serialised json data. What is the best way to exclude this?
As of now, since the serializers.serialize() returns a string, I am doing it as
return re.sub('\"model\"\:\s+?\"getdata\.record_search\"\,', "", data)
But I am sure this is an ugly way to do this. What is the pythonic best way to achieve this?
Upvotes: 0
Views: 98
Reputation: 45565
Serialization is used for serializing/unserializing of models. Django is unable to unserialize model without this "model" field.
Why you use serialization at all? Why not use standard json library?
import json
from getdata.models import record_search
query_data = record_search.objects.filter(**filter_kwargs) \
.values("id", "domain", "record", "record_points_to",
"priority_mx", "ttl", "generated_on")
data = json.dumps(list(query_data))
Upvotes: 1