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']
|
||||
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):
|
||||
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
|
||||
if instance.image:
|
||||
public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')
|
||||
#print(public_url)
|
||||
# remove any ' from the url
|
||||
public_url = public_url.replace("'", "")
|
||||
representation['image'] = f"{public_url}/media/{instance.image.name}"
|
||||
return representation
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue