0

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?

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Catavaran. I was using serialize because, I pass json or xml or yaml through a query string, which I pass it on to serialize, rather than writing separate functions to achieve this. Looks like I'm gonna use your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.