mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
31 lines
No EOL
1.1 KiB
Python
31 lines
No EOL
1.1 KiB
Python
import os
|
|
from .models import Country, Region, VisitedRegion
|
|
from rest_framework import serializers
|
|
|
|
|
|
class CountrySerializer(serializers.ModelSerializer):
|
|
def get_public_url(self, obj):
|
|
return os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/').replace("'", "")
|
|
|
|
flag_url = serializers.SerializerMethodField()
|
|
|
|
def get_flag_url(self, obj):
|
|
public_url = self.get_public_url(obj)
|
|
return public_url + '/media/' + 'flags/' + obj.country_code + '.png'
|
|
|
|
class Meta:
|
|
model = Country
|
|
fields = '__all__' # Serialize all fields of the Adventure model
|
|
read_only_fields = ['id', 'name', 'country_code', 'continent', 'flag_url']
|
|
|
|
class RegionSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Region
|
|
fields = '__all__' # Serialize all fields of the Adventure model
|
|
read_only_fields = ['id', 'name', 'country', 'name_en', 'geometry']
|
|
|
|
class VisitedRegionSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = VisitedRegion
|
|
fields = '__all__' # Serialize all fields of the Adventure model
|
|
read_only_fields = ['user_id', 'id'] |