Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes handling of null product image attributes #343

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

crgwbr
Copy link
Contributor

@crgwbr crgwbr commented Apr 10, 2024

Fixes handling of null product image attributes

When an image product attribute is null, the product API would throw the
following exception:

ValueError: The 'value_image' attribute has no file associated with it.
  
  […]
  
  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "rest_framework/serializers.py", line 686, in to_representation
    return [
  File "rest_framework/serializers.py", line 687, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "oscarapi/serializers/fields.py", line 227, in to_representation
    return value.value.url
  File "django/db/models/fields/files.py", line 66, in url
    self._require_file()
  File "django/db/models/fields/files.py", line 41, in _require_file
    raise ValueError(

This patch fixes this by checking if the image field has a value before trying
to generate a URL.

When an image product attribute is `null`, the product API would throw the
following exception:

```
ValueError: The 'value_image' attribute has no file associated with it.

  […]

  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "rest_framework/serializers.py", line 686, in to_representation
    return [
  File "rest_framework/serializers.py", line 687, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "oscarapi/serializers/fields.py", line 227, in to_representation
    return value.value.url
  File "django/db/models/fields/files.py", line 66, in url
    self._require_file()
  File "django/db/models/fields/files.py", line 41, in _require_file
    raise ValueError(
```

This patch fixes this by checking if the image field has a value before trying
to generate a URL.
@specialunderwear specialunderwear merged commit c89783e into django-oscar:master Apr 12, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants