Review Serializer Documentation
This section outlines the serializers used in the Review API. These serializers handle the transformation of data between complex types (e.g., Django models) and Python data types (e.g., JSON), facilitating the representation of data for API requests and responses.
Serializers
- Review Serializer:
ReviewSerializer - Review Media Serializer:
ReviewMediaSerializer
1. Review Serializer
Serializer Overview
ReviewSerializer is used to serialize review data. It includes information about the review such as the product being reviewed, user details (e.g., first name, last name, photo), the review's rating, headline, text, media, helpful count, and timestamps for when the review was created and last updated.
Fields
| Field | Type | Description |
|---|---|---|
id |
integer |
The unique identifier for the review. |
product |
integer |
The product being reviewed. |
user_first_name |
string |
The first name of the user who wrote the review. |
user_last_name |
string |
The last name of the user who wrote the review. |
user_photo |
string |
The photo of the user who wrote the review. |
rating |
integer |
The rating given by the user (between 1 and 5). |
headline |
string |
The headline or title of the review. |
review_text |
string |
The detailed text of the review. |
review_image1 |
image |
The first image attached to the review. |
review_image2 |
image |
The second image attached to the review. |
review_image3 |
image |
The third image attached to the review. |
review_image4 |
image |
The fourth image attached to the review. |
review_image5 |
image |
The fifth image attached to the review. |
review_video |
file |
A video attached to the review. |
helpful_count |
integer |
The number of users who found this review helpful. |
created_at |
datetime |
The timestamp when the review was created. |
updated_at |
datetime |
The timestamp when the review was last updated. |
is_helpful |
boolean |
Whether the current user has marked this review as helpful. |
Example Response
{
"id": 1,
"product": 100,
"user_first_name": "John",
"user_last_name": "Doe",
"user_photo": "https://example.com/path/to/photo.jpg",
"rating": 5,
"headline": "Great product!",
"review_text": "I absolutely loved this product. It's amazing!",
"review_image1": "https://example.com/path/to/review_image1.jpg",
"review_image2": "https://example.com/path/to/review_image2.jpg",
"review_image3": "https://example.com/path/to/review_image3.jpg",
"review_image4": "https://example.com/path/to/review_image4.jpg",
"review_image5": "https://example.com/path/to/review_image5.jpg",
"review_video": "https://example.com/path/to/review_video.mp4",
"helpful_count": 10,
"created_at": "2024-12-06T10:00:00Z",
"updated_at": "2024-12-06T12:00:00Z",
"is_helpful": true
}
Serializer Methods
ReviewSerializer.get_is_helpful(self, obj)
This method checks if the current authenticated user has marked the review as helpful. It returns True if the user has marked it as helpful and False otherwise.
ReviewSerializer.validate_rating(self, value)
This method validates the rating value to ensure it is a valid rating between 1 and 5, as defined in the RATING_CHOICES of the Review model. If the rating is invalid, it raises a ValidationError.
ReviewSerializer.create(self, validated_data)
This method automatically assigns the authenticated user to the review being created by setting the user field to the current user who is making the request.
2. Review Media Serializer
Serializer Overview
ReviewMediaSerializer is used to serialize the media associated with reviews, including review images and videos.
Fields
- review_image1 (image): The first image attached to the review.
- review_image2 (image): The second image attached to the review.
- review_image3 (image): The third image attached to the review.
- review_image4 (image): The fourth image attached to the review.
- review_image5 (image): The fifth image attached to the review.
- review_video (file): A video attached to the review.
Example Response
{
"review_image1": "https://example.com/path/to/review_image1.jpg",
"review_image2": "https://example.com/path/to/review_image2.jpg",
"review_image3": "https://example.com/path/to/review_image3.jpg",
"review_image4": "https://example.com/path/to/review_image4.jpg",
"review_image5": "https://example.com/path/to/review_image5.jpg",
"review_video": "https://example.com/path/to/review_video.mp4"
}