mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
Fixed image generation in serializer
This commit is contained in:
parent
add2e2bafa
commit
d1a49b7dd9
1 changed files with 19 additions and 9 deletions
|
@ -8,17 +8,27 @@ class AdventureImageSerializer(serializers.ModelSerializer):
|
||||||
fields = ['id', 'image', 'adventure']
|
fields = ['id', 'image', 'adventure']
|
||||||
read_only_fields = ['id']
|
read_only_fields = ['id']
|
||||||
|
|
||||||
|
# def to_representation(self, instance):
|
||||||
|
# representation = super().to_representation(instance)
|
||||||
|
|
||||||
|
# # Build the full URL for the image
|
||||||
|
# request = self.context.get('request')
|
||||||
|
# if request and instance.image:
|
||||||
|
# public_url = request.build_absolute_uri(instance.image.url)
|
||||||
|
# else:
|
||||||
|
# public_url = f"{os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')}/media/{instance.image.name}"
|
||||||
|
|
||||||
|
# representation['image'] = public_url
|
||||||
|
# return representation
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
|
if instance.image:
|
||||||
# Build the full URL for the image
|
public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')
|
||||||
request = self.context.get('request')
|
#print(public_url)
|
||||||
if request and instance.image:
|
# remove any ' from the url
|
||||||
public_url = request.build_absolute_uri(instance.image.url)
|
public_url = public_url.replace("'", "")
|
||||||
else:
|
representation['image'] = f"{public_url}/media/{instance.image.name}"
|
||||||
public_url = f"{os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')}/media/{instance.image.name}"
|
|
||||||
|
|
||||||
representation['image'] = public_url
|
|
||||||
return representation
|
return representation
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue