Skip to content
Snippets Groups Projects
Commit a00bd376 authored by Bodor Máté's avatar Bodor Máté
Browse files

Fix serializer bug

parent 72dc4ded
No related branches found
No related tags found
1 merge request!2Dev
...@@ -8,30 +8,42 @@ _max_count = 1 ...@@ -8,30 +8,42 @@ _max_count = 1
class DocumentSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer):
uploaded_by = serializers.HiddenField(default=CurrentUserProfileDefault()) uploaded_by = serializers.HiddenField(default=CurrentUserProfileDefault())
uploaded_by_name = serializers.SerializerMethodField() uploaded_by_name = serializers.SerializerMethodField()
file = serializers.SerializerMethodField() file_url = serializers.SerializerMethodField()
class Meta: class Meta:
model = models.Document model = models.Document
fields = ( fields = [
'uploaded_by', 'uploaded_by',
'uploaded_at', 'uploaded_at',
'name', 'name',
'description', 'description',
'file_url',
'file', 'file',
'uploaded_by_name', 'uploaded_by_name',
'solution', 'solution',
) ]
read_only_fields = [
'uploaded_by',
'uploaded_at',
'file_url',
'uploaded_by_name',
]
extra_kwargs = {
'file': {'write_only': True},
}
def to_representation(self, instance): def to_representation(self, instance):
data = super().to_representation(instance) data = super().to_representation(instance)
if not data['file']: if not data['file_url']:
data['file'] = "" data['file_url'] = ""
return data return data
def get_uploaded_by_name(self, obj): def get_uploaded_by_name(self, obj):
return obj.uploaded_by.full_name return obj.uploaded_by.full_name
def get_file(self, obj): def get_file_url(self, obj):
return f"/api/v1/documents/{obj.id}/download/" return f"/api/v1/documents/{obj.id}/download/"
def validate_solution(self, value): def validate_solution(self, value):
...@@ -42,5 +54,5 @@ class DocumentSerializer(serializers.ModelSerializer): ...@@ -42,5 +54,5 @@ class DocumentSerializer(serializers.ModelSerializer):
uploaded_by=profile, solution=value).count() uploaded_by=profile, solution=value).count()
if count >= _max_count: if count >= _max_count:
raise serializers.ValidationError( raise serializers.ValidationError(
f'You cant upload more than {max_count} document to one solution!') f'You cant upload more than {_max_count} document to one solution!')
return value return value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment