@@ -147,6 +147,7 @@ class ListingSerializer(ListingTypeMixin, ModelSerializer):
147147 seller = UserSerializer (read_only = True )
148148 listing_type = SerializerMethodField ()
149149 additional_data = SerializerMethodField ()
150+ is_favorited = SerializerMethodField ()
150151 external_link = URLField (required = False , allow_blank = True , allow_null = True )
151152 negotiable = BooleanField (required = False , default = True )
152153 expires_at = DateTimeField (required = False , allow_null = True )
@@ -169,6 +170,7 @@ class Meta:
169170 "images" ,
170171 "listing_type" ,
171172 "additional_data" ,
173+ "is_favorited" ,
172174 ]
173175 read_only_fields = [
174176 "id" ,
@@ -210,6 +212,12 @@ def validate(self, attrs):
210212
211213 return super ().validate (attrs )
212214
215+ def get_is_favorited (self , obj ):
216+ request = self .context .get ("request" )
217+ if not request or not request .user or not request .user .is_authenticated :
218+ return False
219+ return request .user .listings_favorited .filter (id = obj .id ).exists ()
220+
213221 def validate_title (self , value ):
214222 if self .contains_profanity (value ):
215223 raise ValidationError ("The title contains inappropriate language." )
@@ -358,6 +366,7 @@ def _update_sublet(self, instance, additional_data):
358366class ListingSerializerPublic (ListingTypeMixin , ModelSerializer ):
359367 buyer_count = SerializerMethodField ()
360368 favorite_count = SerializerMethodField ()
369+ is_favorited = SerializerMethodField ()
361370 tags = SlugRelatedField (many = True , slug_field = "name" , queryset = Tag .objects .all ())
362371 images = ListingImageURLSerializer (many = True )
363372 seller = UserSerializer (read_only = True )
@@ -381,6 +390,7 @@ class Meta:
381390 "favorite_count" ,
382391 "listing_type" ,
383392 "additional_data" ,
393+ "is_favorited" ,
384394 ]
385395 read_only_fields = fields
386396
@@ -390,6 +400,12 @@ def get_buyer_count(self, obj):
390400 def get_favorite_count (self , obj ):
391401 return obj .favorites .count ()
392402
403+ def get_is_favorited (self , obj ):
404+ request = self .context .get ("request" )
405+ if not request or not request .user or not request .user .is_authenticated :
406+ return False
407+ return request .user .listings_favorited .filter (id = obj .id ).exists ()
408+
393409
394410# Read-only serializer for use when pulling all listings /etc
395411class ListingSerializerList (ListingTypeMixin , ModelSerializer ):
0 commit comments