Open schajee opened 3 years ago
You’re adding fields after django-restql has run, so basically you’re adding fields after django-restql has excluded them. You could use SerializerMethodField
to add other fields as
from rest_framework import serializers
class FileSerializer(serializers.ModelSerializer):
url = serializers.SerializerMethodField()
name = serializers.SerializerMethodField()
size = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
class Meta:
model = File
fields = ['url', 'name', 'size', 'type']
def get_url(self, instance):
return str(instance.file.url)
def get_size(self, instance):
return instance.file.size
def get_name(self, instance):
return str(instance.file.name).split('.')[0]
def get_type(self, instance):
return str(instance.file.name).split('.')[-1]
I have a model setup with a FILE object, and to_representation() to fetch additional file data.
If I try to only include name in the query
query={name}
, the result still includes file{name:...,file"...}
. If I try to omit file using-file
it ignores the query and returns all the fields{id:...,name:...,file"...}
.Any ideas?