Skip to content

ProductWhishListSerializer


Overview

The ProductWhishListSerializer is used for serializing the data related to the user's wishlist. It includes validation to ensure that the same product is not added to the wishlist multiple times and that the product exists in the catalog.

Fields

  • id: The unique identifier of the wishlist entry.
  • user: The user who owns the wishlist. This is read-only and is automatically set based on the current authenticated user.
  • product: The product that is in the wishlist, serialized using the ProductSerializer. This field is read-only.
  • is_wished: A boolean indicating whether the product is currently marked as wished (i.e., added to the wishlist).
  • created_at: The timestamp indicating when the wishlist item was created. This is read-only.

Methods

validate(self, data)

The validate method checks if the product already exists in the wishlist for the authenticated user and ensures that the product ID is provided.

  • Validations:
  • Checks if a valid product ID is provided.
  • Verifies that the product with the given ID exists in the catalog.
  • Ensures that the product is not already in the user's wishlist.

create(self, validated_data)

The create method is overridden to ensure that the new wishlist item is saved correctly with the associated user and product.

  • Logic:
  • The product ID is retrieved, and the corresponding product instance is fetched.
  • A new wishlist item is created with the current authenticated user, the product, and the is_wished flag set to True.