You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a model setup with a FILE object, and to_representation() to fetch additional file data.
class File(models.Model):
name = models.CharField(max_length=100)
file = models.FileField()
class FileSerializer(BaseSerializer):
file = serializers.FileField()
class Meta:
model = File
fields = '__all__'
def to_representation(self, instance):
representation = super().to_representation(instance)
file = {
'url': str(instance.file.url),
'size': instance.file.size,
'name': str(instance.file.name).split('.')[0],
'type': str(instance.file.name).split('.')[-1]
}
representation['file'] = file
return representation
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?
The text was updated successfully, but these errors were encountered:
schajee
changed the title
Django model to_representation() does not honor query
Serializer to_representation() does not honor query
Nov 1, 2021
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
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?
The text was updated successfully, but these errors were encountered: