[Fixed]-Django – get serialized fields from related model

1👍

According to the source, there’s no way to do this using serializers.serialize. The base django serializer will only serialize local_fields on the model instance:

        for field in concrete_model._meta.local_fields:
            if field.serialize:
                if field.remote_field is None:
                    if self.selected_fields is None or field.attname in self.selected_fields:
                        self.handle_field(obj, field)
                else:
                    if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
                        self.handle_fk_field(obj, field)
        for field in concrete_model._meta.many_to_many:
            if field.serialize:
                if self.selected_fields is None or field.attname in self.selected_fields:
                    self.handle_m2m_field(obj, field)

In order to get what you want, you’d have to roll your own serialization function (e.g., something that converts your model to a dict and then uses the DjangoJSONEncoder).

👤2ps

Leave a comment